我刚刚得到一个Arduino并开始学习C ++。我决定首先实施一个简单的汽车躲闪"游戏,使用液晶显示屏和按钮出现在那些very old "9999 in 1" handhelds中。但是我在代码中遇到了一个非常奇怪的问题。
编译器认为我试图覆盖setUp
和tearDown
,但我无法看到这种情况发生在哪里。实际上没有比原始函数定义更多的实例,但是当我尝试编译时会发生这种情况(使用Stino作为Sublime Text,如果相关的话):
Compiling lcd...
Creating C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp.o...
C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp:31: error: 'virtual void Scene::setUp(LiquidCrystal)' cannot be overloaded
C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp:25: error: with 'virtual void Scene::setUp(LiquidCrystal)'
C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp:32: error: 'virtual void Scene::tearDown(LiquidCrystal)' cannot be overloaded
C:\Users\Leonardo\Documents\Arduino_Build\lcd\lcd.ino.cpp:26: error: with 'virtual void Scene::tearDown(LiquidCrystal)'
[Stino - Error 1]
这是"游戏"源文件lcd.ino
,其中没有游戏内容:
#include "LiquidCrystal.h"
#define LEFT_BUTTON_PIN 8
#define RIGHT_BUTTON_PIN 7
#define LEFT_PRESSED B10
#define RIGHT_PRESSED B01
#define STATE_SPLASH 0
#define STATE_PLAY 1
#define STATE_SCORE 2
// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
class Scene {
public:
virtual void loop(byte input, LiquidCrystal lcd) {}
virtual void setUp(LiquidCrystal lcd) {}
virtual void tearDown(LiquidCrystal lcd) {}
};
class Splash : public Scene {
public: void loop(byte input, LiquidCrystal lcd) {
lcd.print("Welcome to CAR DODGE");
lcd.print("Press LEFT to start!");
}
};
class Game : public Scene {
public: void loop(byte input, LiquidCrystal lcd) {
lcd.print("HI");
}
};
class Score : public Scene {
public: void loop(byte input, LiquidCrystal lcd) {
lcd.print("HI");
}
};
byte currentMode = STATE_SPLASH;
Scene *gameModes[] = {new Splash(), new Game(), new Score()};
byte getInput() {
bool leftSample1 = digitalRead(LEFT_BUTTON_PIN);
bool rightSample1 = digitalRead(RIGHT_BUTTON_PIN);
Serial.print(leftSample1);
Serial.print(" ");
Serial.println(rightSample1);
}
void changeGameMode(byte next) {
gameModes[currentMode]->tearDown(lcd);
gameModes[next]->setUp(lcd);
currentMode = next;
}
void setup() {
Serial.begin(9600);
pinMode(LEFT_BUTTON_PIN, INPUT);
pinMode(RIGHT_BUTTON_PIN, INPUT);
lcd.begin(20, 4);
changeGameMode(STATE_SPLASH);
}
void loop() {
byte input = getInput();
gameModes[currentMode]->loop(input, lcd);
delay(150);
}
编辑: lcd.ino.cpp内容,由Arduino构建工具生成:
#include <Arduino.h>
#include "LiquidCrystal.h"
#define LEFT_BUTTON_PIN 8
#define RIGHT_BUTTON_PIN 7
#define LEFT_PRESSED B10
#define RIGHT_PRESSED B01
#define STATE_SPLASH 0
#define STATE_PLAY 1
#define STATE_SCORE 2
// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
class Scene {
public:
virtual void setUp(LiquidCrystal lcd);
virtual void tearDown(LiquidCrystal lcd);
byte getInput();
void changeGameMode(byte next);
virtual void loop(byte input, LiquidCrystal lcd) {}
virtual void setUp(LiquidCrystal lcd) {}
virtual void tearDown(LiquidCrystal lcd) {}
};
class Splash : public Scene {
public: void loop(byte input, LiquidCrystal lcd) {
lcd.print("Welcome to CAR DODGE");
lcd.print("Press LEFT to start!");
}
};
class Game : public Scene {
public: void loop(byte input, LiquidCrystal lcd) {
lcd.print("HI");
}
};
class Score : public Scene {
public: void loop(byte input, LiquidCrystal lcd) {
lcd.print("HI");
}
};
byte currentMode = STATE_SPLASH;
Scene *gameModes[] = {new Splash(), new Game(), new Score()};
byte getInput() {
bool leftSample1 = digitalRead(LEFT_BUTTON_PIN);
bool rightSample1 = digitalRead(RIGHT_BUTTON_PIN);
Serial.print(leftSample1);
Serial.print(" ");
Serial.println(rightSample1);
}
void changeGameMode(byte next) {
gameModes[currentMode]->tearDown(lcd);
gameModes[next]->setUp(lcd);
currentMode = next;
}
void setup() {
Serial.begin(9600);
pinMode(LEFT_BUTTON_PIN, INPUT);
pinMode(RIGHT_BUTTON_PIN, INPUT);
lcd.begin(20, 4);
changeGameMode(STATE_SPLASH);
}
void loop() {
byte input = getInput();
gameModes[currentMode]->loop(input, lcd);
delay(150);
}
我在这里缺少什么?
答案 0 :(得分:1)
答案可能有点晚,但是;
似乎Arduino IDE不喜欢在类定义中声明的成员函数。
要明确:
class example {
public:
void function(){ /*code not here*/ };
private:
int variable;
};
void example::function(){
//code here
}
答案 1 :(得分:0)
从MS C ++帮助:
必须定义基类中的虚函数,除非使用pure-specifier声明它们。
我认为基类中的null定义可能是问题所在。使基类声明为纯,并删除空定义,或提供诊断非空定义。
答案 2 :(得分:0)
在我的特殊情况下,发生错误是因为我不小心多次声明了相同的函数。 一旦我删除了额外的函数声明,问题就解决了。