Hi,
I am trying to give an object as an argument to a function, but I am getting an error:
class myclass
{
public:
void yo() {
Serial.println("Hello");
}
};
myclass pbs=myclass();
void reportit(pbs& i) {
//i.yo(); // If this didn't give an error, I would uncomment this method call.ß
Serial.print("test");
};
void setup()
{
Serial.begin(115200); Serial.println("---------------------------------------");
};
void loop() {
};
but I get this error:
tmp:-1: error: variable or field 'reportit' declared void
tmp:-1: error: 'pbs' was not declared in this scope
tmp:-1: error: 'i' was not declared in this scope
tmp:11: error: variable or field 'reportit' declared void
tmp:11: error: 'i' was not declared in this scope
I believe it's a quirk of the Arduino IDE that a class will need to be defined in its own header file, somename.h and #included into your code. Because of this, your actual code for the class will need to be kept in its own sketch with a file suffix .cpp as a library in essence.
myclass pbs=myclass(); //pbs is a variable name, you cannot use it as a type. ie:
void reportit(pbs& i);
//It must be a type: myclass, which pbs is just a variable of...
void reportit(myclass& i);
Ok, thanks... I got it now. But I don't get the separation of files. I have created classes in the sketch without issue.
My understanding of including files goes like this: The C preprocessor finds the directive, and for all intents and purposes, what the C++ compiler is presented with is essentially the entire text of the included files, along with the file that contains the include directive, in one flow of textual data.
What difference does pulling the class out of the sketch make? ...I mean, technically. What is going on under the covers that we don't see?
There is a good response in your older thread by Jantje explaining why the code doesn't work. Its not a c++ error, but due to the way the IDE modifies the sketch before compiling.