How to give an Object as an Argument to a Function? (...not to a class method)

I am trying to give an object as an argument to a function, but I am getting an error:

class myclass
  void yo() {

myclass pbs=myclass();

void reportit(pbs& i) {
  //i.yo(); // If this didn't give an error, I would uncomment this method call.ß

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

…Can you tell me why?


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.

So, split out your class into a library and you should be fine. Here's a guide to how that's done

Cheers, Geoff

You coded

void reportit(pbs& i) {

when I think you meant

void reportit(pbs *i) {

which tells the compiler to expect i to be a pointer to a pbs.

I have given a valid response in your other post,88031 you can see how I separated the files.

Your error is:

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);

also define an instance like:

myclass pbs;


myclass pbs=myclass();

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?

Thanks again for your response(s).

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.,88031.msg661456.html#msg661456

Ok, yes that makes sense now I suppose. I'm not totally happy about it, but at least I understand.

Thanks again!