Pages: [1]   Go Down
Author Topic: How to give an Object as an Argument to a Function? (...not to a class method)  (Read 445 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I am trying to give an object as an argument to a function, but I am getting an error:
Code:
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

...Can you tell me why?
Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 29
Posts: 1071
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

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

Logged

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

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You coded
Code:
void reportit(pbs& i) {
when I think you meant
Code:
void reportit(pbs *i) {
which tells the compiler to expect i to be a pointer to a pbs.
Logged

There's always a better way!

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
myclass pbs;

not

Code:
myclass pbs=myclass();
« Last Edit: January 21, 2012, 10:11:43 am by pYro_65 » Logged


0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

http://arduino.cc/forum/index.php/topic,88031.msg661456.html#msg661456
Logged


0
Offline Offline
Jr. Member
**
Karma: 4
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanks again!
Logged

Pages: [1]   Go Up
Jump to: