I am making a tiny game, and want to store high-scores. I made a Highscore-class in the top of the sketch file, like this:
class Highscore {
public:
String name;
int score;
String toString() {
return this->name + " - " + (String)score;
}
Highscore(String name, int score) {
this->name = name;
this->score = score;
}
};
setup() { // etc
Now. Near the bottom of the sketch, I have a function I want to take a Highscore-object as a parameter, but the compiler will not let me. Example:
void writeHighscore(Highscore hs) {
File f = SD.open("high.txt", FILE_WRITE);
// Do stuff
The complation error is as follows:
sketch.ino:19:21: error: variable or field ‘writeHighscore’ declared void
sketch.ino:19:21: error: ‘Highscore’ was not declared in this scope
sketch.ino: In function ‘void setup()’:
sketch.ino:64:20: error: ‘writeHighscore’ was not declared in this scope
After changing parameter type to String, int or whatever, all the errors go away. I am able to create Highscore-objects INSIDE the writeHighscore()
function, but how can I pass objects to it?
Edit 1 - Object creation
Highscore hs = Highscore("John", 975);
Serial.println(hs.toString()); // Prints "John - 975" as expected
writeHighscore(hs); // Error
&hs
approach beforee writing the post, as well as passing the pointerwriteHighscore(&hs)
->void writeHighscore(Highscore *hs)
but to no avail.