Nicolas Epaminonda
Nicolas Epaminonda

Reputation: 191

C/C++ - Nested Class Inheriting from Enclosing Class

I would like to use nested classes as a part of an application I am building. The first piece of code I have (header file, which I included some code for this question) is the following:

class Window {

public:
    indev::Thunk32<Window, void ( int, int, int, int, void* )> simpleCallbackThunk;
    Window() {
        simpleCallbackThunk.initializeThunk(this, &Window::mouseHandler); // May throw std::exception
    }
    ~Window();

    class WindowWithCropMaxSquare;
    class WindowWithCropSelection;
    class WindowWithoutCrop;

    virtual void mouseHandler( int event, int x, int y, int flags, void *param ) {
    printf("Father");
    }

private:
    void assignMouseHandler( CvMouseCallback mouseHandler );    

};

class Window::WindowWithCropMaxSquare : public Window {

public:
    WindowWithCropMaxSquare( char* name );
    virtual void mouseHandler( int event, int x, int y, int flags, void *param ) {
        printf("WWCMS");
    }

};

class Window::WindowWithCropSelection : public Window {

public:
    WindowWithCropSelection( char* name );
    virtual void mouseHandler( int event, int x, int y, int flags, void *param ) {
        printf("WWCS");
    }

};

class Window::WindowWithoutCrop : public Window {

public:
    WindowWithoutCrop( char* name );
    virtual void mouseHandler( int event, int x, int y, int flags, void *param ) {
        printf("WWOC");
    }

};

Now, I want to instantiate a WindowWithCropMaxSquare class in MAIN and execute the mouseHandler function.

In MAIN I have

Window::WindowWithCropMaxSquare *win = new Window::WindowWithCropMaxSquare("oopa");
win->mouseHandler(1,1,1,1,0);

However, this causes a problem at the linking stage. I got the following error:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Window::WindowWithCropMaxSquare::WindowWithCropMaxSquare(char *)" (??0WindowWithCropMaxSquare@Window@@QAE@PAD@Z) referenced in function _main c:\Users\Nicolas\documents\visual studio 2010\Projects\AFRTProject\AFRTProject\AFRTProject.obj

So, can anyone please let me know how to address this problem?

Upvotes: 1

Views: 313

Answers (1)

Oliver
Oliver

Reputation: 29483

You need two things: a body for each constructor, and correct const'ness.

WindowWithCropMaxSquare( char* name );

is just a declaration without any definition (body). Empty constructor body, as you imply in your comment, would be

WindowWithCropMaxSquare( char* name ) {}

Also I very much suspect that

Window::WindowWithCropMaxSquare *win = new Window::WindowWithCropMaxSquare("oopa");

requires a constructor that takes const char* since you are giving it a constant (an rvalue):

WindowWithCropMaxSquare( const char* name ) {}

or

WindowWithCropMaxSquare( const string& name ) {}

The compiler will not give a constant as argument to a function that takes non-const since such function is indicating to you that it may modify the given argument, clearly not allowed for a constant.

Upvotes: 3

Related Questions