Very strange problem in very basic code

Why would this not work:

void uselessFunctionDeclarationThatWillCauseError(int a) {}
typedef struct { char deviceName[15]; } SomeType;
int saveConfig(SomeType &something) {}
void setup() {}
void loop() {}

It will give errors:

/home/merc/Arduino/sketch_may5b/sketch_may5b.ino:3:16: error: 'SomeType' was not declared in this scope
 int saveConfig(SomeType &something) {}
                ^~~~~~~~
/home/merc/Arduino/sketch_may5b/sketch_may5b.ino:3:26: error: 'something' was not declared in this scope
 int saveConfig(SomeType &something) {}
                          ^~~~~~~~~
/home/merc/Arduino/sketch_may5b/sketch_may5b.ino: In function 'int saveConfig(SomeType&)':
/home/merc/Arduino/sketch_may5b/sketch_may5b.ino:3:35: error: 'int saveConfig(SomeType&)' redeclared as different kind of symbol
 int saveConfig(SomeType &something) {}
                                   ^
/home/merc/Arduino/sketch_may5b/sketch_may5b.ino:3:5: note: previous declaration 'int saveConfig'
 int saveConfig(SomeType &something) {}
     ^~~~~~~~~~

exit status 1

Compilation error: 'SomeType' was not declared in this scope

Whereas this identical code, with the uselessFunctionDeclarationThatWillCauseError function out, will compile fine:

typedef struct { char deviceName[15]; } SomeType;
int saveConfig(SomeType &something) {}
void setup() {}
void loop() {}

I am at my wits end with this.

you are victim of Arduino's team trying to make the artists less dependant of C++ rules which say that "things" need to be declared before they are being used.

The IDE is trying to be smart and generates declarations for the functions (prototypes) you use before compiling and dump them at the beginning of the file or where it thinks it's appropriate, but sometimes get confused when you defined types or stuff not at the expected place. if you move the typedef at the top, then you won't get a compile error

typedef struct {
  char deviceName[15];
} SomeType;

void uselessFunctionDeclarationThatWillCauseError() {}
int saveConfig(SomeType something) {}
void setup() {}
void loop() {}

PS: in C++ you don't need typedef for a struct, you can just write

struct SomeType {
  char deviceName[15];
};
2 Likes

My guess would be that apparently this:

will be interpreted as a specification and not just a declaration, which apparently causes the saveConfig to also be interpreted as such.

While the code without the first 'useless' function is taken as a declaration only (despite the {} on saveConfig) and as such, you don't get to the point of getting the same error.

Although @J-M-L's explanation is really just...better.

there are a few GitHub bugs and forum's posts about this

for exemple

1 Like

This killed my Sunday morning programming session... I am so. Angry.
:frowning:

Once you know it it's not that bad.. Most of the time the IDE gets it right

You can also put your own function prototype somewhere after the typedef and before the function, that prevents the IDE from auto-generating a prototype for that particular function.

Yeah, I am calming down, trying to get this beast finished by tonight...
I am NOT a C/C++ developer. I have 30+ years experience in software development, but NOT in C/C++. I am limping my way towards polished code, but boy, it's hard work...
I wish you could program this with Rust...

Can we help?

Yep I just wrestled another similar issue... This time I think I am overwriting memory all over the shop. This is getting more and more fun...

This is my last head-banging exercise...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.