Error when using struct in a function

Hi,

I've got the following bit of code (simplified for the problem):

struct myStruct {
  double a;
  double b;
};

myStruct aStruct = {0,1.5};

//double getA( myStruct &aStruct) {
//  return aStruct.a;
//}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Starting");
}

void loop() {
  // put your main code here, to run repeatedly: 
  double printMe;
 // printMe = getA(aStruct);
  Serial.println(printMe);
}

Like this it runs, so the initialization part of the struct works fine, but when I uncomment the function getA() (and in the loop()), I get the following error:

postUp:-1: error: 'myStruct' was not declared in this scope
postUp:-1: error: 'aStruct' was not declared in this scope
postUp.cpp: In function 'double getA(myStruct&)':
postUp:7: error: 'double getA(myStruct&)' redeclared as different kind of symbol
postUp:-1: error: previous declaration of 'double getA'
postUp.cpp: In function 'void loop()':
postUp:20: error: 'getA' cannot be used as a function

Can anyone shed some light on this? I can't see where I could possibly be redeclaring anything....

Try using * instead of & and -> instead of .:

double getA(struct myStruct *aStruct) {
  return aStruct->a;
}

postUp:7: error: 'double getA(myStruct&)' redeclared as different kind of symbol
postUp:-1: error: previous declaration of 'double getA'

These two messages should give you a clue. The Arduino IDE creates function prototypes for all functions. It fails to do so properly for functions that use reference parameters. So, you need to define the function prototype even though you define the function before you reference it. Doing so will prevent the IDE from trying to "help" you define it.

Thanks,

found the problem through Majenko's snippet, though it's actually something else: need to declare struct in the function as

double getA(struct myStruct &aStruct) {
  return aStruct.a;
}

I had written getA(myStruct &aStruct) :{|

Cheers,
lifesayko

lifesayko:
need to declare struct in the function

I much prefer to declare a typedef for the struct type so that you can treat it just like any other type.

As the .ino is essentially a cpp and GCC is C++, not C. Typedef makes no difference as a named struct is its type-name also ( even an un-named struct is still an anonymous type ).

The problem may be how the IDE pre-processes your sketch and generates prototypes. This creates problems with functions that use user-defined types or enumerations.

If you define your class or structure in a separate tab as a .h file, then #include that in your main or other tab, you can use it.

I.e.: main sketch tab:

#include "foo.h"

myStruct aStruct = {0,1.5};

double getA( myStruct &aStruct) {
return aStruct.a;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Starting");
}

void loop() {
  // put your main code here, to run repeatedly: 
  double printMe;
 printMe = getA(aStruct);
  Serial.println(printMe);
}

foo.h tab:

struct myStruct {
  double a;
  double b;
};

compiles.