我正在尝试计算有多少个实例或类mole2
,并将该数字存储在名为mole_count的公共静态变量中。
mole2.h
#ifndef mole2_h
#define mole2_h
#include "Arduino.h"
class mole2 {
public:
mole2(int input, int output);
void popUp();
void popdown();
boolean moleBrainThinkPopUpNow();
void setUpTimer(int up_timer);
boolean didMoleGetHit();
void setRecoveryTimer(int recovery_timer);
void decrementRecoveryTimer();
boolean dosePlayerMistMole();
void moleReset();
int input, output;
static int mole_count;
static int odds_of_poping;
private:
boolean _is_popped = false;
int _up_timer = 0;
int _recovery_timer = 0;
};
#endif
mole2.cpp
#include "Arduino.h"
#include "mole2.h"
int mole2::odds_of_poping = 10;
mole2::mole2(int input, int output) {
input = input;
output = output;
int mole2::mole_count = mole2::mole_count + 1;
pinMode(input, INPUT);
pinMode(output, OUTPUT);
}
void mole2::popUp() {
_is_popped = true;
digitalWrite(input, HIGH);
}
void mole2::popdown() {
_is_popped = false;
digitalWrite(input, LOW);
}
boolean mole2::moleBrainThinkPopUpNow() {
if (_recovery_timer == 0 && _is_popped == false && rand() % odds_of_poping == 1) {
popUp();
return true;
}
else {
return false;
}
}
void mole2::setUpTimer(int up_timer) {
_up_timer = up_timer;
}
boolean mole2::didMoleGetHit() {
if (_is_popped == true && digitalRead(input) == HIGH) {
popdown();
_up_timer = 0;
return true;
}
else {
return false;
}
}
void mole2::setRecoveryTimer(int recovery_timer) {
_recovery_timer = recovery_timer;
}
void mole2::decrementRecoveryTimer() {
if (_recovery_timer > 0) {
_recovery_timer--;
}
}
boolean mole2::dosePlayerMistMole() {
if (_is_popped == true && _up_timer > 0) {
_up_timer--;
}
if (_is_popped == true && _up_timer == 0) {
popdown();
return true;
}
else {
return false;
}
}
void mole2::moleReset() {
popdown();
_up_timer = 0;
_recovery_timer = 0;
}
FullError
Arduino: 1.6.12 (Windows 7), Board: "Arduino/Genuino Uno"
C:\Users\Strings\Documents\Arduino\libraries\mole2\mole2.cpp: In constructor 'mole2::mole2(int, int)':
C:\Users\Strings\Documents\Arduino\libraries\mole2\mole2.cpp:9:25: error: qualified-id in declaration before '=' token
int mole2::mole_count = mole2::mole_count + 1;
^
exit status 1
Error compiling for board Arduino/Genuino Uno.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
答案 0 :(得分:0)
你必须做与odds_of_poping
相同的事情:
定义:
int mole2::mole_count = 0; // initialization can be ommited as static variables are initialized to the default value
以后只需增加:++mole_count;
答案 1 :(得分:0)
int mole2::mole_count
已经定义。这是试图制作另一个int mole2::mole_count
。丢失int
并分配给预先存在的变量。
您还需要为mole_count
分配存储空间并对其进行初始化。放置
int mole2::mole_count = 0; // replace 0 if starting with more than 0 mole2s
在某处
int mole2::odds_of_poping = 10;
in mole2.cpp
还要注意
input = input;
output = output;
编译器不知道您是否要使用传递的参数input
或类input
,因此它将选择最接近的定义input
,参数,在=的两侧。避免在同一时间重复使用相同的变量名称。
这可以通过明确说明来解决
this->input = input;
this->output = output;
或者(我最喜欢的!)使用member initializer list
mole2::mole2(int input, int output): input(input), output(output) {
但是为了防止其他混乱,你仍然不应该重复这些名字。