Example that FunctionA calls FunctionB, that will generate an error if FunctionB is below FunctionA in the code.
But by declare FunctionB in the beginning of the sketch this issue is resolved.
Example Top of sketch
void DisplayClear(Rectangle &bounds);
Later somewhere in the code
void DisplayClear(Rectangle &bounds) {
// Clear the display rectangle
}
How can I achieve the same result with a class in the sketch?
Example class
class SensorLight: public Thread {
private:
bool isDaylight;
public:
SensorLight() {
isDaylight = false;
}
bool IsDaylight() {
return isDaylight;
}
word GetBackgroundColor() {
return (IsDaylight()) ? 0x801000 : 0xff3333;
}
word GetFontColor() {
return (IsDaylight()) ? 0x000000 : 0x33ffcc;
}
void run(){
// do some light stuff
runned();
}
};
So I would like to declare this in the top of the sketch. Tried this
class SensorLight {
public:
word GetBackgroundColor() {return 0xffffff;}
word GetFontColor() {return 0xffffff;}
};
SensorLight light = SensorLight();
But I'm getting the error
arduino_sketch:494: error: redefinition of 'class SensorLight'
arduino_sketch:147: error: previous definition of 'class SensorLight'
arduino_sketch:902: error: redefinition of 'SensorLight light'
arduino_sketch:152: error: 'SensorLight light' previously declared here
declaring functions
How can I achieve the same result with a class in the sketch?
Because compiler reads from top to down all classes and functions needs to be placed in an order for the compiler to work.
If you trying to call a function before it's declared you get a scope error.
This can be solved by just declaring the function name and properties.
I'm wondering if it's possible to do the same with a class.
Guessing like a *.h file.
Maybe another example that can show what I want to accomplish.
class SensorLight {
public:
word GetBackgroundColor() {return 0xffffff;}
word GetFontColor() {return 0xffffff;}
};
// some code here
class Text
{
private:
public:
SensorLight* light;
Text() {}
void Print(char* val) {
word fcolor = light.GetFontColor();
}
};
// some more code
class SensorLight
{
private:
bool isDaylight;
public:
SensorLight() {
isDaylight = false;
}
bool IsDaylight() {
return isDaylight;
}
word GetBackgroundColor() {
return (IsDaylight()) ? 0x801000 : 0xff3333;
}
word GetFontColor() {
return (IsDaylight()) ? 0x000000 : 0x33ffcc;
}
};
SensorLight light = SensorLight();
Text myText = Text();
myText.light = &light;
This gives me the error
sketch_jun26a.ino: In member function 'void Text::Print(char*)':
sketch_jun26a:20: error: request for member 'GetFontColor' in '((Text*)this)->Text::light', which is of non-class type 'SensorLight*'
sketch_jun26a.ino: At global scope:
sketch_jun26a:26: error: redefinition of 'class SensorLight'
sketch_jun26a:3: error: previous definition of 'class SensorLight'
sketch_jun26a:49: error: expected constructor, destructor, or type conversion before '.' token
I think the second declaration is intended to be a class definition, and the OP has been misled by some examples that define inline methods within the class declaration. The problem would be avoided by defining any inline methods in the first declaration of the class, and replacing the second declaration with definitions for all the methods/members which are not inline.
PeterH:
I think the second declaration is intended to be a class definition, and the OP has been misled by some examples that define inline methods within the class declaration. The problem would be avoided by defining any inline methods in the first declaration of the class, and replacing the second declaration with definitions for all the methods/members which are not inline.
The problem would best be avoided by doing it right, and defining the class in a header file and implementing it in a cpp file.
So the answer is 'No'.
You can't declare a class in Arduino sketch as you can with functions?
Wrong. You can. Whether the class definition is in the sketch file or in a separate header file does not matter. Whether the implementation is in the sketch file or in a separate source file does not matter.
What DOES matter is that you define the class ONCE. If you are planning to implement some functions in one place (in the class definition) and others in another place, you must use the scope resolution operator when implementing the methods, NOT another class statement.
So how can I declare a class for later on type the correct one? Can you give an example.
An example of how to do it with functions.
// declare the function
void DisplayClear(Rectangle &bounds);
// use the function
DisplayClear( Rectangle( 0,0,100,100) );
// here is the actual function
void DisplayClear(Rectangle &bounds) {
// do some stuff as clear the display
}
Because compiler reads from top to down all classes and functions needs to be placed in an order for the compiler to work.
If you trying to call a function before it's declared you get a scope error.
That is not true of functions when using the Arduino IDE. Consider this
In the code as presented funcA() is called before it is defined, but it works. Behind the scenes the function prototypes are added to the program as part of the process of checking or uploading the code.
It is possible to pre-declare class names in the same way as functions
class myClass ;
void myfunction ( myClass* arg ) ; // at this point, the details of class are not known, but the compiler knows there is a class
// with that name, which can have a pointer to it.
// more stuff
// more stuff
class myClass
{
// actual class definition
}
You sometimes need to do this, if class A refers to class B and class B also refers to class A.