gpt4 book ai didi

c++ - 无法在静态类函数中递增静态变量

转载 作者:行者123 更新时间:2023-11-28 05:37:06 26 4
gpt4 key购买 nike

class Stepper
{
private:
int enable;
int direction;
int speed;
static int turretCounter;
public:
Stepper(int en, int dir)
{
enable = en;
direction = dir;
pinMode(enable,OUTPUT);
pinMode(direction,OUTPUT);
pinMode(4,OUTPUT);
}
void Stop()
{
digitalWrite(enable,1);
Timer1.detachInterrupt();
Timer1.stop();
}
static void IncrementCounter()
{
turretCounter++;
}
//your step pin must be 4 due to limitations on static vars and methods
static void Step()
{
digitalWrite(4,0);
delayMicroseconds(1);
digitalWrite(4,1);
IncrementCounter();
}
void SetSpeed(int Speed)
{
speed = Speed;
}
void Run(int seconds)
{
digitalWrite(enable,0);
Timer1.attachInterrupt(Step);
Timer1.initialize(speed);
}
int GetCounter()
{
return turretCounter;
}
void SetDirection(int dir)
{
digitalWrite(direction,dir);
}
int GetSpeed()
{
return speed;
}
};

我想提请您注意静态 Step() 方法和静态 IncrementCounter() 方法。我试过在 Step() 函数中增加变量,但我总是得到“Arudino Nano 板编译错误”。我尝试直接在 Step() 方法中调用 IncrementCounter(),但出现“无法调用没有对象的类方法”错误。我从 Step() 方法中得到这个错误。

最佳答案

尝试在某处定义turretCounter(最好在.cpp/.cxx/.C 文件中),例如这个:

int Stepper::turretCounter = 0;

如果这解决了您的问题,那么您可能忽略了一条信息更丰富的错误消息并且只发布了编译器输出的最后一行。以后要更加注意编译错误,并养成按照它们在输出中出现的顺序研究它们的习惯(因为后面的错误可能只是前面错误的结果)。

关于c++ - 无法在静态类函数中递增静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38016980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com