tags:

views:

41

answers:

1

For those who are following the saga, I am still trying to define Finite State Machine, states & events in the "proper" C++ way, with templates.

What's wrong with this code?

template <typename StateTypeEnum, typename EventTypeEnum>
class Fsm
{
public:
    Fsm(E_subSystems subSystem,
        uint8_t instance,
        const char * const fsmName,
        const std::vector<State<StateTypeEnum, EventTypeEnum> >& states)
    {}

where

template <typename StateTypeEnum, typename EventTypeEnum>
class State
{
public:
    State(INPUT E_subSystems subSystem,
    StateTypeEnum stateId, 
    const char * const stateName,
    const std::map<Event<EventTypeEnum>,  EventHandlerFunction>& events)
{}

The only error message is

no matching function for call to "State<E_callControlStates, E_callControEvents>::State()" fsm.h line 98 C/C++ Problem

It looks like the error message refers to a non-existent default constructor for state, but why?

E_callControlStates, E_callControEvents were the template parameters for declaring an object of Fsm (with no errors).

Obviously, I am overlooking something & making a st00pid n00b mistake, but what? Thanks in advance


My bad. Of course it had nothing to do with the code that I was looking at - but then it rarely does, does it?

The class Fsm declared

private:    State<StateTypeEnum, EventTypeEnum>  _currentState;

when it should have been

private:    State<StateTypeEnum, EventTypeEnum> *_currentState;

Sorry for misleading you, folks, and thanks for deducing the problem, despite that.

+1  A: 

The problem is not in the code you present, but most probably a member of type State that is not being initialized in the initialization list of some constructor, forcing the compiler to default initialize it, and the compiler is not finding the appropriate constructor.

I can only assume that line 98 is in the Fsm constructor and that Fsm has a member of type State<...>.

David Rodríguez - dribeas
+! I am sure that you are on the correct track about constructor problems. yes, line 98 is the FSM constructor. FSM has a member `std::vector<State<StateTypeEnum, EventTypeEnum> > _states;`I don't use initializer lists in this code (which is bad, I know), but the STL might.
Mawg
The problem is **not** using intializer lists. The vector should not be a problem (unless you request an initial size, or resize it --as compared to push_back elements), but if you had a State subobject that does not have a default constructor, you will be forced to use initializer lists.
David Rodríguez - dribeas