Pages: [1]   Go Down
Author Topic: Passing Object to function  (Read 1384 times)
0 Members and 1 Guest are viewing this topic.
Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why will this not compile, I believe it is plain enough c++.
Passing an object reference to a function is very important to my coding style smiley

Code:
class John
{
    public:
    int a;
};

John j;

// the Following line in Arduino 1.0.2 causes:
// error: 'John was not declared in this scope
void doJohn(John & j1){  int i=j1.a;    } 

void setup()
{
    John &j2(j);  // this works ok
    int i= j2.a;
}

void loop()
{

}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50366
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you put the definition of the class in a header file, and include that file in the sketch, the code compiles.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why will this not compile, I believe it is plain enough c++.

The problem is in the 'mucking about' that Arduino does with your code before compiling it. If you have functions that are not prototyped then it creates prototypes for you and puts them in front of your code. In this case, that means before your type/class declarations. The declarations are inserted after your #includes, so one way to avoid the problem is to put the prototypes in a #included file.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It also compiles if you change:
Code:
void doJohn(       John & j1){  int i=j1.a; }
to
Code:
void doJohn(class John & j1){  int i=j1.a; }

This trick works for 'struct' so I gave it a try with 'class' and it worked.  Probably works with 'enum', too.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It also compiles if you change:

I wasn't aware that worked too. Does it implicitly declare 'class John;'?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why will this not compile, I believe it is plain enough c++.

The problem is in the 'mucking about' that Arduino does with your code before compiling it. If you have functions that are not prototyped then it creates prototypes for you and puts them in front of your code. In this case, that means before your type/class declarations. The declarations are inserted after your #includes, so one way to avoid the problem is to put the prototypes in a #included file.

Very good explanation, Thank You!

John
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It also compiles if you change:
Code:
void doJohn(       John & j1){  int i=j1.a; }
to
Code:
void doJohn(class John & j1){  int i=j1.a; }

This trick works for 'struct' so I gave it a try with 'class' and it worked.  Probably works with 'enum', too.

Oh that's a great point.  I should have tried that myself!

Cheers,
John
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It also compiles if you change:
I wasn't aware that worked too. Does it implicitly declare 'class John;'?

I suspect it just provides enough context for the compiler to make a valid forward reference.  If later the type "John" was declared as something else (struct John, or typedef int John) the compiler would likely complain.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: