I've just got an Arduino and started learning C++. I decided to start by implementing a simple "car dodge" game, present in those very old "9999 in 1" handhelds using a LCD and pushbuttons. However I encountered a very strange issue with the code.
The compiler believes I'm trying to override setUp
and tearDown
, but I can't see where this is happening. There is literally no further instances than the original function definitions, yet this is what happens when I try to compile (using Stino for Sublime Text, if that's relevant):
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]
This is the "game" source file, lcd.ino
, which has nothing of game yet:
#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);
}
EDIT: lcd.ino.cpp contents, which is generated by the Arduino build tools:
#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);
}
What am I missing here?
Answer might be a little late but;
It appears the Arduino IDE doesn't like member functions being declared in the class definition.
Just to be clear:
class example {
public:
void function(){ /*code not here*/ };
private:
int variable;
};
void example::function(){
//code here
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments