2

I'm trying to run an arduino program that uses a struct containing servo objects and it gives me this error:

error: 'leg' does not name a type

I think I'm doing something wrong with memory management but I'm fairly new to this so any help is appreciated.

This is my code:

#include <Servo.h> 

typedef struct{
 Servo hip;
 Servo shin;
 Servo foot;
}leg;



int currentPin = 0; //this is the pin that the leg will be attached to


leg getLeg(void){
  leg newLeg;
  newLeg.hip.attach(currentPin++);
  newLeg.shin.attach(currentPin++);
  newLeg.foot.attach(currentPin++);

  return newLeg;  

}

void setup() 
{ 
  leg frontLeft = getLeg();
  leg frontRight = getLeg();
  leg backRight = getLeg();
  leg backLeft = getLeg();
} 


void loop() 
{ 


} 
Jordan
  • 1,564
  • 2
  • 13
  • 32

2 Answers2

2

Try this instead

struct legtype {
    Servo hip;
    Servo shin;
    Servo foot;
};

typedef legtype leg;

Does that work?

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • 1
    You've tagged the question with 'C' - is it really so or are compiling as C++? In any case, why `typedef` the `struct` instead of just using `struct leg newLeg;`? – Anders R. Bystrup Nov 13 '12 at 08:30
  • Yes Its C, I only know C and I've been fine so far. Unless.. could it be C++? – Jordan Nov 13 '12 at 08:38
  • Ok I've fixed it by using struct leg. Thanks for the help – Jordan Nov 13 '12 at 08:45
  • 1
    Yes... Have a look at this SO thread explaining the intricacies of `typedef`'s in C and C++: http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c – Anders R. Bystrup Nov 13 '12 at 08:47
2

defining the struct in a header file solved this for me. In arduino make a new tab named : "whatever.h" define the struct in whatever.h include whatever.h in the main file.

Melvin Sovereign
  • 455
  • 5
  • 10