Reputation: 512
In the following code, the avr-g++ (Arduino IDE) compiler throws the error: 'ControllerPosition' does not name a type
. What is the problem with the code?
struct ControllerPosition
{
int y, x;
ControllerPosition(int _y = 0x7FFF, int _x = 0x7FFF) : y(_y), x(_x) {}
};
ControllerPosition mapPosition(int input)
{
return ControllerPosition((input % 10) * 2 + 1, (input / 10) * 2 + 1);
}
Upvotes: 0
Views: 510
Reputation: 512
I have found the source of the issue. Arduino IDE finds the functions in a given file puts the prototypes of them at the beginning of a file, which means it puts the prototypes before the struct definition and the function uses the struct as its return type. Therefore the solution was to explicitly declare the prototypes after the definition of the struct.
i.e.
struct ControllerPosition
{
int y;
int x;
};
ControllerPosition mapPosition(int input);
ControllerPosition mapPosition(int input)
{
return ControllerPosition((input % 10) * 2 + 1, (input / 10) * 2 + 1);
}
PS: It is possible to declare the prototypes before the struct by making a class forward declaration struct ControllerPosition;
.
Upvotes: 0
Reputation: 11012
I'm not sure there is a problem with the code posted. The following works for me:
struct ControllerPosition {
int y,x;
ControllerPosition(int _y = 0x7FFF,int _x = 0x7FFF) : y(_y),x(_x) {}
};
ControllerPosition mapPosition(int input)
{
return ControllerPosition((input % 10) * 2 + 1,(input / 10) * 2 + 1);
}
int main()
{
auto testvar = mapPosition(4);
return 0;
}
Have a look at this post discussing a similar error in more generic terms.
Upvotes: 0