'XXX' was not declared in this scope

I've run across a strange error. I suspect it has something to do with how .ino files are processed. Here's my code (in a .ino file):

void g() {}
struct XXX { int x; };
void f(XXX s) {}
void setup() {}
void loop() {}

And here are the errors that occur when it is compiled:

....sketch_aug15a.ino:3:8: error: variable or field 'f' declared void
void f(XXX s) {}
^~~
....sketch_aug15a.ino:3:8: error: 'XXX' was not declared in this scope

exit status 1

Compilation error: variable or field 'f' declared void

The error goes away if I delete void g() {}.

It also goes away if I used the struct keyword before XXX s, but that shouldn't be necessary.

It also goes away if I precede the definition of f() with a declaration of f() using the same syntax.

Names can be changed and the error stays, e.g. "f" can be changed to another name.

Any ideas?

I have moved your topic to Programming Questions....More suitable for the issue then Arduino IDE 2.x.

I confirm this also happens with wokwi.

struct XXX { int x; };
void g() {}
void f(XXX s) {}
void setup() {}
void loop() {}

While this way it compiles without errors.

It's from the Arduino IDE's auto-prototype generator being fooled. You can fix it by moving the struct declaration as shown in the previous post or adding a superfluous prototype for the f() function:

void g() {
}

struct XXX {
  int x;
};

void f(XXX s);
void f(XXX s) {
}

void setup() {
}

void loop() {
}
1 Like

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