views:

97

answers:

1

This code gives error C2504: 'IKeyEvent': base class undefined on line 3.

class IKeyEvent;

class EventDispatcher : private IKeyEvent {
public:
    enum EEActions {
        A_FEW_ACTIONS
    };
private:
    void OnKey(EventDispatcher::EEActions action, char multiplier);
}

class IKeyEvent {
public:
    virtual void OnKey(EventDispatcher::EEActions action, char multiplier) = 0;
};

You cannot inherit from a class until it is defined, understandably. But I cannot define IKeyEvent until after EventDispatcher is defined.

I know I can move that enum out of the Event Dispatcher definition to make it global but this would require refactoring a large portion of the program. Is there any way to have EventDispatcher inherit from a class dependent on EventDispatcher?

+5  A: 

My recommendation: move EEActions into the base class - it is part of the interface, after all:

class IKeyEvent {
public:
    enum EEActions {
        A_FEW_ACTIONS
    };
    virtual void OnKey(EEActions action, char multiplier) = 0;
};

class EventDispatcher : public IKeyEvent {
private:
    void OnKey(EventDispatcher::EEActions action, char multiplier);
};

If you then also make the inheritance from IKeyEvent public, you can continue to refer to the enum as EventDispatcher::EEActions (despite the enum being defined in the base type).

Martin v. Löwis