struct definition error but only if a function is previously defined

  arduino, c++

This simple piece of Arduino (C++) code fails with error myType does not define a type.
It works if I comment out the doNothing function, or if I move the function after the definition of the struct.

What’s going on?


//it works if this is commented out
void doNothing() { }

struct myType {
  int x;
};

myType f(){ //error here: myType does not define a type
  myType m;
  m.x = 5;
  return m;
}

void setup() {
  myType p = f();
  int x = p.x;
}

void loop() {
}

Source: Windows Questions C++

LEAVE A COMMENT