在声明数组之后,所有元素都以某种方式成为最后一个元素
After declaring an array, all elements somehow become the last element
基本上,我正在构建一些代码,其中对象数组(我自己创建了对象)需要在类范围内可访问,但在一些其他步骤之后初始化。这是我所做的:
基对象
unsigned char stepPin;
unsigned char dirPin;
unsigned char sensorPin;
const char* ident;
//Object
StepperMotor :: StepperMotor(const unsigned char _stepPin,
const unsigned char _dirPin,
const unsigned char _sensorPin,
const char* _ident)
{
stepPin = _stepPin;
dirPin = _dirPin;
sensorPin = _sensorPin;
ident = _ident;
std::cout << "Motor " << ident << ": Step Pin - " << (int)stepPin << " | Direction Pin - " << (int)dirPin << std::endl;
}
对象管理器
#include "stepperMotor.h"
#include <iostream>
#include <wiringPi.h>
#include <thread>
#include <cstdlib>
#include <vector>
#include "stepperManager.h"
StepperMotor motors[] = {
{7, 15, 16, "HS"},
{0, 1, 2, "HL"},
{3, 4, 5, "HP"},
{12, 13, 6, "CS"},
{14, 10, 11, "VP"},
{21, 22, 26, "TC"},
{23, 24, 27, "TR"},
{25, 28, 29, "IN"}
};
StepperManager::StepperManager()
{
for (int i = 0; i < 8; i++){
motors[i].dumpData();
}
}
现在到实际问题…
初始声明之后,数组中的所有元素都成为最后一个元素。您可以通过查看运行时的输出来了解这一点:
输出:Motor HS: Step Pin - 7 | Direction Pin - 15
Motor HL: Step Pin - 0 | Direction Pin - 1
Motor HP: Step Pin - 3 | Direction Pin - 4
Motor CS: Step Pin - 12 | Direction Pin - 13
Motor VP: Step Pin - 14 | Direction Pin - 10
Motor TC: Step Pin - 21 | Direction Pin - 22
Motor TR: Step Pin - 23 | Direction Pin - 24
Motor IN: Step Pin - 25 | Direction Pin - 28
Declare the motor man
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
所以,我不完全理解为什么会发生这种情况,我试过使数组静态,并切换到向量,但没有任何帮助。恐怕我对语言的了解不够,无法自己发现问题。
编辑
有人指出我漏掉了实际的运行代码。对不起,伙计们。
这是主"实现对象的文件。
#include <iostream> // For cout and cerr
#include <cstdlib> // For atoi()
#include <cstring>
#include "stepperManager.h"
int main(int argc, char **argv)
{
std::cout << "Declare the motor man" << std::endl;
StepperManager motorMan;
return 0;
}
这里您需要了解更多关于类及其工作原理的知识。在StepperMotor's
源文件中,您使用外部链接定义文件作用域全局变量。每次你构造一个StepperMotor
时,你都在覆盖那些相同的变量,所以所有的StepperMotors
都有效地访问相同的值(因此你看到的行为)。
因为你有c#背景,就像你在这里为StepperMotors
使用静态成员变量一样。您需要非静态成员变量。简单的例子:
foo:
// Foo.h
#ifndef FOO_H
#define FOO_H
class Foo
{
public:
explicit Foo(int value);
void print_value() const;
private:
int member_variable;
}
#endif
Foo.cpp:
// Foo.cpp
#include "Foo.h"
#include <iostream>
using namespace std;
Foo::Foo(int value): member_variable(value)
{
}
void Foo::print_value() const
{
cout << member_variable << endl;
}
main.cpp:
// main.cpp
#include "Foo.h"
int main()
{
Foo f1(123);
Foo f2(456);
Foo f3(789);
f1.print_value();
f2.print_value();
f3.print_value();
}
输出:123
456
789
我也看到一些线程的使用在你的例子。在这一点上,我想说,这就像耍刀片一样。你首先要坚持基本的,掌握语言的诀窍,调试器,然后你可以循序渐进地走向并行执行,用手边的分析器进行微调优,等等。
您已经声明了四个全局变量,每次创建StepperMotor
时都要更改它们。相反,您希望这些是类成员,以便每个对象都有自己的副本,独立于其他对象中的副本:
class StepperMotor {
unsigned char stepPin;
unsigned char dirPin;
unsigned char sensorPin;
const char* ident;
// and constructors, member functions, etc.
};
我想你的步进电机应该有这样的东西。h
class StepperMotor{
//some more stuff
public:
unsigned char stepPin;
unsigned char dirPin;
unsigned char sensorPin;
const char* ident;
unsigned int stepVal;
}
这是c++中声明成员变量的方式。我认为阅读一些c++教程来学习基础知识可能会很有用。
相关文章:
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果第一个元素包含任何零,则我的程序以不希望的方式运行
- C++通过别名指针以静默方式将错误的类型分配给数组元素
- 对列表列表中的元素进行分组的最佳方式
- 是否有任何优雅的方式来遍历元素位置可以更改的列表?
- 使用现代C++在 Eigen3 矩阵上使用 for 循环进行元素操作的优雅方式
- 如何使用指针访问成对向量的元素
- 如何以简洁的方式安全地访问容器中的每个第 n 个元素?
- 如何将数组的每个n个元素组合成一个元素
- 以C++递归方式对链表中的元素求和
- 链接列表中元素的成对交换(通过更改指针)
- c++ 双向链表的相反元素的成对乘法
- 以编程方式推断出枚举的大小,独立于其元素的价值
- 以重复的方式将元素添加到 std::vector
- 如果连接了换成气泡排序的元素,请找到所有非连接元素的最大子集
- 干燥的方式来构建具有相同初始化器列表的数组的所有元素
- 如何以正确的方式找到向量中的元素-C
- 通过删除C 中的评论,额外的空格和空白行,从文本文件中换成元素
- 有没有任何标准的算法来进行向量元素的成对比较
- 将一组元素分解成最少数量的回文