Go Down

Topic: How to give an Object as an Argument to a Function? (...not to a class method) (Read 849 times) previous topic - next topic


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

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 http://arduino.cc/en/Hacking/LibraryTutorial

Cheers, Geoff

"There is no problem so bad you can't make it worse" - retired astronaut Chris Hadfield

Morris Dovey

You coded
Code: [Select]
void reportit(pbs& i) {
when I think you meant
Code: [Select]
void reportit(pbs *i) {
which tells the compiler to expect i to be a pointer to a pbs.
There's always a better way!


I have given a valid response in your other post http://arduino.cc/forum/index.php/topic,88031
you can see how I separated the files.

Your error is:

Code: [Select]

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:
Code: [Select]
myclass pbs;


Code: [Select]
myclass pbs=myclass();
Forum Mod anyone?


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.

Forum Mod anyone?


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

Thanks again!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131