我剛剛得到了一個Arduino並開始學習C++。我決定開始實施一個簡單的「汽車閃避」遊戲,使用LCD和按鈕在very old "9999 in 1" handhelds中出現。不過,我遇到了一個代碼非常奇怪的問題。「錯誤:'A'不能超載」,但代碼中沒有任何超載
編譯器認爲我試圖覆蓋setUp
和tearDown
,但我無法看到發生了什麼。從字面上有比原來的函數定義沒有進一步的情況,然而,這是當我嘗試編譯發生了什麼(使用Stino爲崇高的文本,如果這是相關的):
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);
}
我在這裏錯過了什麼?
這是完整的源文件'lcd.ino.cpp'? – Columbo 2014-10-28 12:12:04
您能否指出顯示的代碼中第25,26,31和32行的哪一行? – 2014-10-28 12:14:09
@JoachimPileborg我也不知道,行號似乎也在完整的代碼中。我將發佈整個文件。編輯:現在問題包含整個文件,但行號根本沒有意義。 – Kroltan 2014-10-28 12:14:53