#ifndef _MOTOR_HPP
#define _MOTOR_HPP
#include "State_machine.hpp"
// structure to hold event data passed into state machine
struct Motor_data : public Event_data
{
int speed;
};
// the Motor state machine class
class Motor : public State_machine
{
public:
Motor() : State_machine(ST_MAX_STATES) {}
// external events taken by this state machine
void halt();
void set_speed(Motor_data*);
private:
// state machine state functions
void ST_idle(Event_data*);
void ST_stop(Event_data*);
void ST_start(Motor_data*);
void ST_change_speed(Motor_data*);
// state map to define state function order
public:
const (State_struct::*p_state_func) * get_state_map()
{
staticconst (State_struct::*p_state_func) state_map[] =
{ { reinterpret_cast<State_struct::*p_state_func>(&Motor::ST_idle) }
,{ reinterpret_cast<State_struct::*p_state_func>(&Motor::ST_stop) }
,{ reinterpret_cast<State_struct::*p_state_func>(&Motor::ST_start) }
,{ reinterpret_cast<State_struct::*p_state_func>(&Motor::ST_change_speed) } };
return state_map;
}
// state enumeration order must match the order of state
// method entries in the state map
enum E_states {
ST_IDLE, // 0
ST_STOP, // 1
ST_START, // 2
ST_CHANGE_SPEED, // 3
ST_MAX_STATES // 4
};
};
#endif // _MOTOR_HPP
When I compile, I get these errors:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Motor.d" -MT"src/Motor.o" -o "src/Motor.o" "../src/Motor.cpp"
In file included from ../src/Motor.hpp:3:0,
from ../src/Motor.cpp:2:
../src/State_machine.hpp:22:47: error: ISO C++ forbids declaration of ‘p_state_func’ with no type [-fpermissive]
virtual const (State_struct::*p_state_func) * get_state_map() = 0;
^
../src/State_machine.hpp:22:47: error: ‘p_state_func’ declared as a ‘virtual’ field
../src/State_machine.hpp:22:47: error: expected ‘;’ at end of member declaration
../src/State_machine.hpp:22:69: error: ISO C++ forbids declaration of ‘get_state_map’ with no type [-fpermissive]
virtual const (State_struct::*p_state_func) * get_state_map() = 0;
^
../src/State_machine.hpp:22:51: error: initializer specified for non-virtual method ‘int* State_machine::get_state_map()’
virtual const (State_struct::*p_state_func) * get_state_map() = 0;
^~~~~~~~~~~~~
In file included from ../src/Motor.cpp:2:0:
../src/Motor.hpp:29:40: error: ISO C++ forbids declaration of ‘p_state_func’ with no type [-fpermissive]
const (State_struct::*p_state_func) * get_state_map()
^
../src/Motor.hpp:29:40: error: expected ‘;’ at end of member declaration
../src/Motor.hpp:29:58: error: ISO C++ forbids declaration of ‘get_state_map’ with no type [-fpermissive]
const (State_struct::*p_state_func) * get_state_map()
^
../src/Motor.hpp: In member function ‘int* Motor::get_state_map()’:
../src/Motor.hpp:31:9: error: expected primary-expression before ‘static’
static const (State_struct::*p_state_func) state_map[] =
^~~~~~
../src/Motor.hpp:36:16: error: ‘state_map’ was not declared in this scope
return state_map;
^~~~~~~~~
../src/Motor.hpp:36:16: note: suggested alternative: ‘get_state_map’
return state_map;
^~~~~~~~~
get_state_map
Don't know where you got this code from; but note that using the results of the reinterpret casts to call the functions would engender undefined behaviour.
First, I thank you for your 'repairing', I fiddled at the code a more than a half day long.
Don't know where you got this code from; but note that using the results of the reinterpret casts to call the functions would engender undefined behaviour.
What would you suggest for improving, e.g avoiding the reinterpret_cast?