Variable or field declared as void

I have the following (very simplified) code:

class Color {
  public:
  Color(int r, int g, int b):
  r(r), g(g), b(b){};
  private:
  int r, g, b;
};

Color red(255,0,0);

void setColor(Color c){};

void setup(){
}
void loop(){
}

However if I try to compile this code I get the following output:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from testing2.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"
^
testing2.cpp:3:15: error: variable or field 'setColor' declared void
void setColor(Color c);
^
testing2.cpp:3:15: error: 'Color' was not declared in this scope
make: *** [testing2.o] Error 1

The source is very stripped down from my actual application. I was able to compile everything yesterday, but today, nothing works.

I first tried it with the color class in its own file, but that wasnā€™t working either.

The version of the firmware iā€™m compiling against hasnā€™t changed: SPARK FIRMWARE V0.3.3 (SEP 9)

I think you are having trouble with the Spark/Arduino pre-processor which tries to handle forward references to variables and function prototype declarations for you.

Try turning off the pre-processor with this pragma at the top of your file:

#pragma SPARK_NO_PREPROCESSOR

2 Likes

That worked!
My dad and I were wracking our brains for a couple hours trying to figure this out.

Thank you!

Any idea what could be done so that this wouldnā€™t have to be used?

1 Like

My understanding is that the Spark/Arduino pre-processor is not called on .h/.cpp files but is on .ino files unless you include that pragma.

It is designed to help novice programmers so they donā€™t have to learn all the rules of C/C++ but it can definitely cause weird problems with classes and structs and typedefs.

I put the pragma in my larger codebase, which includes a #include to my Color class and now Iā€™m being told that:

testing.cpp:3:2: warning: #import is a deprecated GCC extension [-Wdeprecated]
#import "Color.h"
^

Any thoughts?

Edit: Iā€™m just now noticing that itā€™s also ignoring the pragma

If you are compiling on the web IDE as I think you are, then you should have a .ino file as your main program with setup() and loop(). Only this file needs the pragma.

If you have added other files via the circle plus in the upper corner, they should be .h and .cpp files. You need to follow the C++ rules with the class defined in the .h header file and include the header in the .cpp and in your sketch (.ino file). You should not need the pragma in these files. It is good to use an ifdef or once in the header file so that you donā€™t load the header file more than once.

Is there a reason itā€™s ignoring the pragma?

Also, now that iā€™ve put it in there my pin definitions are ā€˜not declared in this scopeā€™

It is not ignoring your pragma, particularly if the pins are not defined. You also need to add #include "application.h" to your file now since you are writing pure C/C++ when the pragma is in place. Sorry, should have mentioned that.

That may fix a bunch of things!

2 Likes

I have the same problem but the #pragma SPARK_NO_PREPROCESSOR didnt work for me.
This is my .cpp file

#include "PushingBox.h"
#include "application.h"

String _devid;
void PushingBox::sendPush(_devid) {

        client.stop();

        client.connect("api.pushingbox.com", 80); {

        client.print("GET /pushingbox?devid=");
        client.print(_devid);
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.println("api.pushingbox.com");
        client.println("User-Agent: Spark");

        client.println();
        client.flush();
}
}

This is my .h file

#ifndef PUSHINGBOX
#define PUSHINGBOX

#include "application.h"

namespace PushingBox
{
  class PushingBox
  {

    public:
    void sendPush(String _devid);
  };
}

#endif

and this is my .ino file

#pragma SPARK_NO_PREPROCESSOR
#include "PushingBox/PushingBox.h"

#include "application.h"



void setup() {
    sendPush("27a623784");

}

void loop() {

}

I hope you can help me!
thx

Since you put your class in the namespace PushingBox, you have to use that namespace to access it.

Try PushingBox::sendPush("27a623784"); in your .ino file instead.

You are going to have to do more than just call client.flush() to get rid of the returned data from the web server in my experience. Flush just flushes the local buffer but doesnā€™t handle all the data effectively.

1 Like

Thank you for the fast answer.
I changed my .ino file but the error stayed.
this is the exact compiler error:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from PushingBox/PushingBox.h:5,
from PushingBox/PushingBox.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"
^
PushingBox/PushingBox.cpp:6:33: error: variable or field 'sendPush' declared void
void PushingBox::sendPush(_devid) {
^
make: *** [PushingBox/PushingBox.o] Error 1

Iā€™m a bit puzzled about this

#include "PushingBox
#include "application.h"

String _devid;                // <-- can we drop this and  
void PushingBox::sendPush(_devid) // <-- should this not be (String _devid) ?
{

        client.stop();

        client.connect("api.pushingbox.com", 80); 
        {  // what statement does this { belong to?
          client.print("GET /pushingbox?devid=");
          client.print(_devid);
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.println("api.pushingbox.com");
          client.println("User-Agent: Spark");
    
          client.println();
          client.flush();
        }
}
1 Like

In your cpp file, the above is a problem, you wanted


void PushingBox::sendPush(String _devid) {

1 Like

I`m overwhelmed by your fast response!
I changed my code, now only 1 Error stayed

PushingBox/PushingBox.cpp:6:40: error: 'void PushingBox::sendPush(String)' should have been declared inside 'PushingBox'
void PushingBox::sendPush(String _devid) {
^

I dont get where I shoud have declared PushingBox?
Thanks.

I guess this might clear up a bit once you name the namespace and the class slightly different.
Iā€™d understand inside 'PushingBox' as inside the namespace, while PushingBox::sendPush() denotes sendPush() as part of the class PushingBox.

But I might be wrong :wink:

See


1 Like

I tried a lot, but I realized my C++ skills are just too low.
The code is on Github
You can post your Github username and I make you a contributor.
I hope someone can fix it because it would be an easy way to send push notifications from your spark core.
If the code works one day I`ll add documentations and add it to Spark IDE.
Have a nice evening.

Iā€™ve located several issues in your code, but Iā€™d consider it more effective, if youā€™ll solve them bit by bit yourself.
But as some hints:

Your error message from above will be solved by wrapping a namespace PushingBox { ... } around your method definitions in your .cpp.
But Iā€™d strongly suggest to either rename your namespace or your class. It will prevent ambiguity and will help you to understand error messages easier.

You are calling PushingBox(devid) but you have no constructor in your class (and no such method either).

Since your sendPush() expects a String youā€™ll have to make sure that you actually pass it a String and not a number type.

And finally since you havenā€™t defined your method static, youā€™ll have to instanciate an object of your class before you can call any non-static method.

I hope this helps you take the next few steps and youā€™re alway welcome to ask more questions.

1 Like