Hello Guys!!!
I'm having a trouble compiling the following code.
Actually it works fine on my desktop computer.
But the avr-g++ gives me the following error messages.
Can anybody help me out? Please.....
Thanks in advance....
class Machine {
class State *current;
public:
Machine();
void setCurrent(State *s){
current = s;
}
void on();
void off();
void red();
};
class State {
public:
virtual void on(Machine *m){ Serial.println(" already ON" );}
virtual void off(Machine *m){ Serial.println(" already OFF" );}
virtual void red(Machine *m){ Serial.println(" already RED" );}
};
void Machine::off(){ current->off(this);}
void Machine::on(){ current->on(this);}
void Machine::red(){ current->red(this);}
class ON: public State {
public:
void red(Machine *m);
};
class OFF: public State {
public:
void on(Machine *m){
m->setCurrent(new ON());
delete this;
}
};
class RED: public State {
public:
void off(Machine *m){
m->setCurrent(new OFF());
delete this;
}
};
void ON::red(Machine *m){
m->setCurrent(new RED());
delete this;
}
Machine::Machine(){
current = new OFF();
}
const char btn[3]={22,23,24};
void (Machine::*ptrs[])()= {
Machine::off,
Machine::on,
Machine::red
};
Machine fsm;
char num[3];
void setup(){
for (int i=0; i<3; i++)
pinMode(btn[i], INPUT);
pinMode(13, OUTPUT);
}
void loop() {
for (int i=0; i<3; i++)
num[i] = digitalRead(btn[i]);
if(num == 1 || num == 0 || num == 2)
(fsm.*ptrs[num])();
}
statePattern:57: error: invalid use of non-static member function 'void Machine::off()'
Machine::off,
^
statePattern:58: error: invalid use of non-static member function 'void Machine::on()'
Machine::on,
^
statePattern:59: error: invalid use of non-static member function 'void Machine::red()'
Machine::red