views:

231

answers:

4

consider this class:

class baseController {

    /* Action handler array*/

std::unordered_map<unsigned int, baseController*> actionControllers;

protected:

    /**
     *  Initialization. Can be optionally implemented.
     */
    virtual void init() {

    }

    /**
     * This must be implemented by subclasses in order to implement their action
     * management
     */
    virtual void handleAction(ACTION action, baseController *source) = 0;

    /**
     * Adds an action controller for an action. The actions specified in the
     * action array won't be passed to handleAction. If a controller is already
     * present for a certain action, it will be replaced.
     */
    void attachActionController(unsigned int *actionArr, int len,
      baseController *controller);

    /**
     *
     * checks if any controller is attached to an action
     *
     */
    bool checkIfActionIsHandled(unsigned int action);

    /**
     *
     *  removes actions from the action-controller filter.
     *  returns false if the action was not in the filter.
     *  Controllers are not destoyed.
     */
    bool removeActionFromHandler(unsigned int action);

public:

    baseController();

    void doAction(ACTION action, baseController *source);

};

}

and this subclass

class testController : public baseController{

    testController tc;

protected:

    void init(){
     cout << "init of test";
    }



    void handleAction(ACTION action, baseController *source){

     cout << "nothing\n";

    }

};

The compiler comes out with an error on the subclass on the member

testController tc;

..saying

error: field ‘tc’ has incomplete type

but if I remove that and I instatiate the class it works... is there a way to avoid this error??? It looks so strange to me....

A: 

It won't compile because you're declaring a member variable 'tc' that is an instance of itself. You're not using tc in the subclass; what is your intent here?

Jeff Paquette
A: 

You can not create an object of the class inside that class itself. Probably what you intend to do is to keep a pointer to the class. In that case you should use it as testController* BTW, why do you want to do that? It looks a bit strange to me.

Naveen
I was just testing the base class... it's intented to hold many controllers of the same type
gotch4
+5  A: 

Your code is trying to embed an entire instance of testController inside itself, which is impossible. Instead, you want a reference:

testController &tc;

or a pointer

testController *tc;
Mike Seymour
You mentioned in another question that you're familiar with Java, but new to C++. One important difference is the meaning of a declaration of a class variable, such as `Class object;`. In C++, this variable is an actual instance of `Class`, rather than a reference as it is in Java.
Mike Seymour
+6  A: 
one day someone asked me why a class can't contain an instance of itself and i said;
  one day someone asked me why a class can't contain an instance of itself and i said;
    one day someone asked me why a class can't contain an instance of itself and i said;
      ...

use indirection. a (smart) pointer or refrence to a testController rather than a testController.

jk