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:
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.
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.
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.
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.
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
#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();
}
}
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.
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.