2014-10-28 25 views
1

我剛剛得到了一個Arduino並開始學習C++。我決定開始實施一個簡單的「汽車閃避」遊戲,使用LCD和按鈕在very old "9999 in 1" handhelds中出現。不過,我遇到了一個代碼非常奇怪的問題。「錯誤:'A'不能超載」,但代碼中沒有任何超載

編譯器認爲我試圖覆蓋setUptearDown,但我無法看到發生了什麼。從字面上有比原來的函數定義沒有進一步的情況,然而,這是當我嘗試編譯發生了什麼(使用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); 
} 

我在這裏錯過了什麼?

+2

這是完整的源文件'lcd.ino.cpp'? – Columbo 2014-10-28 12:12:04

+0

您能否指出顯示的代碼中第25,26,31和32行的哪一行? – 2014-10-28 12:14:09

+0

@JoachimPileborg我也不知道,行號似乎也在完整的代碼中。我將發佈整個文件。編輯:現在問題包含整個文件,但行號根本沒有意義。 – Kroltan 2014-10-28 12:14:53

回答

1

答案可能有點遲,但;

看來Arduino IDE不喜歡在類定義中聲明的成員函數。

只是要清楚:

class example { 
    public: 
      void function(){ /*code not here*/ }; 
    private: 
      int variable; 
}; 

void example::function(){ 
    //code here 
} 
+0

的確是這個問題!也許對我來說已經很晚了,但我相信有人會從這個答案中受益! – Kroltan 2015-03-07 01:09:44

0

從MS C++幫助:

Virtual functions in a base class must be defined unless they are declared using the pure-specifier.

我想在基類中的定義無效可能是問題。可以使基類聲明爲純粹的,並刪除空定義,或提供診斷非空定義。

+0

但他們*是*定義的。即使成員函數的主體是空的,它仍然意味着成員函數將被定義。 – 2014-10-28 15:22:34

+0

@joachim這就是我所期望的。不過,我看不到任何其他問題,所以值得檢查它是否是MS的編譯器怪癖/差異。所呈現的錯誤與中間文件具有聲明後跟定義有關 - 似乎認爲定義是過載。但爲什麼? – davidc 2014-10-28 15:41:27

+0

那麼Arduino幾乎不使用Microsoft C++編譯器。 – 2014-10-28 15:58:48