[Solved] Class "not declared in this scope"

This is likely a C++ issue, and my lack of familiarity with it, but I haven’t figured this out, nor found a solution online. Here is a simple program

class A {
};

class B {
    A a;
    void doSomething(A& a) {  }
};

void setup() {
}

void loop() {
}

The error message is

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from classproblem.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
classproblem.cpp:3:18: error: variable or field 'doSomething' declared void
void doSomething(A& a);
^
classproblem.cpp:3:18: error: 'A' was not declared in this scope
classproblem.cpp:3:21: error: 'a' was not declared in this scope
void doSomething(A& a);
^

I think the bit about variable or field declared void is a misleading side effect of the problem, because if I remove the parameter from doSomething(), it compiles without error.

If I change the defintion of doSomething to “void doSomething(A a) { }”, then the error becomes

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from classproblem.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
classproblem.cpp:3:18: error: variable or field 'doSomething' declared void
void doSomething(A a);
^
classproblem.cpp:3:18: error: 'A' was not declared in this scope
make: *** [classproblem.o] Error 1

Can anyone point me to the proper fix for this?


I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

There might be several things that could interfere with this code.
Since this code actually does absolutely nothing the optimizer might get in your way and just do away with some things and also the “Spark” preprocessor sometimes gives you some headache.

Try to add some dumb/dummy functionality to prevent the over optimisation.
And for the preproc, try #pragma SPARK_NO_PREPROCESSOR but then you’d need to #include "application.h"

1 Like

This is an extremely simplified example from a real program. However, your suggestion eliminated the error, both in this simple example and in my real program. It makes me wonder what else the preprocessor is doing. Do I miss anything significant by not running it?

As far as I know you won’t miss a lot.

It mainly does some stuff to help people who are not familiar with C/C++ like adding function prototypes.
But it only treats .ino files anyway - .cpp/.c/.h files are untouched.

1 Like