在声明数组之后,所有元素都以某种方式成为最后一个元素

After declaring an array, all elements somehow become the last element

本文关键字:元素 方式成 最后一个 之后 数组 声明      更新时间:2023-10-16

基本上,我正在构建一些代码,其中对象数组(我自己创建了对象)需要在类范围内可访问,但在一些其他步骤之后初始化。这是我所做的:


基对象

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++教程来学习基础知识可能会很有用。