Hi all,
I have a class (Flag) that uses an array[3] of objects of another class (Color). Color has an enum for 3 colors.
In Flags, I want to be able to set the colors.
Here is the .h code:
#ifndef BugEnum_h
#define BugEnum_h
class Color {
public:
Color();
enum Colors {RED, WHITE, BLUE};
Colors color = BLUE;
Colors _color = RED;
virtual void setColor(Colors newColor);
private:
};
class Flag {
public:
Flag();
Color flagColors[3];
private:
virtual void setFrance();
};
#endif
Here is the .cpp code:
#include "Arduino.h"
#include "BugEnum.h"
Color::Color();
void Color::setColor(Colors newColor) {_color = newColor; }
Flag::Flag();
void Flag::setFrance() {
flagColors[0].color = BLUE;
flagColors[1]._color = WHITE;
flagColors[2].setColor(RED);
}
I get that RED, WHITE and BLUE are not declared in this scope.
How can I get them in scope of Flag?
Thanks for your help
Shouldn't Flags inherit from Colors?
Actually not.
I just want to use three Color objects, like I would use three Servo objects.
Jacques
It seems that the problem arises because of the array.
I added 2 lines with a Color member of Flag and there is no warning from the compiler for them.
The .h code:
#ifndef BugEnum_h
#define BugEnum_h
class Color {
public:
Color();
enum Colors {RED, WHITE, BLUE};
Colors color = RED;
Colors _color = RED;
virtual void setColor(Colors newColor);
private:
};
class Flag {
public:
Flag();
Color flagColors[3];
Color _rope; //<<<<<<<ADDED
private:
virtual void setFrance();
};
#endif
The .cpp code:
#include "Arduino.h"
#include "BugEnum.h"
Color::Color();
void Color::setColor(Colors newColor) {_color = newColor; }
Flag::Flag();
void Flag::setFrance() {
flagColors[0].color = BLUE;
flagColors[1]._color = WHITE;
flagColors[2].setColor(RED);
_rope.color = BLUE; //<<<<ADDED
}
Now, that is strange.
No problem doing it in the sketch either (even with the array):
#include "BugEnum.h"
Color testColor[3];
void setup() {
// put your setup code here, to run once:
testColor[0].color = WHITE;
}
void loop() {
// put your main code here, to run repeatedly:
}
very, very strange.
@BulldogLowell . Have you seen Post #4 ?
This is something we can easily do. Create an array of objects. The code I posted there works. I have used it numerous times.
But in a class, it does not seem to work. Maybe I am clumsy about it, but there should be a way that a class can use an array of an object of an other class.
No?
void Flag::setFrance() {
flagColors[0].color = Color::BLUE;
flagColors[1]._color = Color::WHITE;
flagColors[2].setColor(Color::RED);
}
system
September 13, 2017, 9:27am
9
Why is the enum Color a class member? Make the stupid thing global!
Thank you very much for your help with this syntax, PaulMurrayCbr.
Really appreciate it.