I'm no expert on the intricacies between C++ & Java so i'm hoping to learn something here. I found that writing a class definition after void loop() results in an error because the compiler has to see the definition before the class can be implemented. For example:
void setup() {}
void loop() {
someClass myClass;
}
class someClass{};
results in the error 'someClass' was not declared in this scope.
But when working with Processing, the below works:
void setup(){}
void draw(){
someClass myClass = new someClass();
}
class someClass{}
My questions are:
- Why does defining classes after the loop portion work in Processing but not Arduino?
- If I want a class to be in my sketch instead of a library, does it always have to be before void loop()? What is the best practice for using a class with Arduino?
- Why doesn't this limitation extend to functions? e.g I can safely write a function after void loop().
I think that because a class is a kind of variable, it must be declared before you can use it, just like any variable.
As to functions, the IDE (usually) automatically produces function prototypes. Function prototypes are function declarations.
Thanks for your answer, I think I understand a bit better now.
Is it then possible to create a "class prototype", if such a thing exists, at the start of the code while leaving the definition after the void loop()?
Why not just put the class in a .h file and #include it ?
UKHeliBob:
Why not just put the class in a .h file and #include it ?
I could and probably should, but my intention is to understand why the alternative is not possible.
And sometimes for small files I'd rather just write a class within the source.
Wangmaster:
I could and probably should, but my intention is to understand why the alternative is not possible.
And sometimes for small files I'd rather just write a class within the source.
The alternative is not possible because as you said in your original post
writing a class definition after void loop() results in an error because the compiler has to see the definition before the class can be implemented.
Perhaps what you should be asking is why that is not necessary in Processing.
Don't forget that the Arduino IDE lets you commit all kinds of C programming "sins" such as using functions before they are declared by creating them for you, the object of which is to make it easier to use for beginners. However, even that does not for for functions with a default value for one or more parameters so be grateful for what does work.
Wangmaster:
Is it then possible to create a "class prototype", if such a thing exists, at the start of the code while leaving the definition after the void loop()?
Sure it is, that's what a .h file usually does:
class MyClass {
public:
MyClass(uint8_t i);
void setVrivateVar(uint8_t v);
uint8_t getPrivateVar();
private:
uint8_t privateVar;
};
MyClass myObject(50);
void setup() {
Serial.begin(115200);
Serial.println(myObject.getPrivateVar());
myObject.setVrivateVar(random(256));
Serial.println(myObject.getPrivateVar());
}
void loop() {
}
MyClass::MyClass(uint8_t i) : privateVar(i) {}
void MyClass::setVrivateVar(uint8_t v) {
privateVar = v;
}
uint8_t MyClass::getPrivateVar() {
return privateVar;
}
All you have to do is declare it BEFORE you use it
Mark