C / C ++ - 嵌套类继承自封闭类

时间:2011-12-30 06:30:47

标签: c++ class inheritance member nested

我想使用嵌套类作为我正在构建的应用程序的一部分。我拥有的第一段代码(头文件,我在其中包含了一些此代码的代码)如下:

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");
    }

};

现在,我想在MAIN中实例化一个WindowWithCropMaxSquare类并执行mouseHandler函数。

在MAIN我有

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

但是,这会在链接阶段引起问题。我收到以下错误:

  

错误1错误LNK2019:未解析的外部符号“public:__thiscall Window :: WindowWithCropMaxSquare :: WindowWithCropMaxSquare(char *)”(?? 0WindowWithCropMaxSquare @Windows @@ QAE @ PAD @ Z)在函数_main c:\ Users \中引用Nicolas \ documents \ visual studio 2010 \ Projects \ AFRTProject \ AFRTProject \ AFRTProject.obj

那么,任何人都可以让我知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

你需要两件事:每个构造函数的主体和正确的const'ness。

WindowWithCropMaxSquare( char* name );

只是一个没有任何定义(正文)的声明。您在评论中暗示的空构造函数体将是

WindowWithCropMaxSquare( char* name ) {}

我也非常怀疑

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

需要一个带const char*的构造函数,因为你给它一个常量(一个rvalue):

WindowWithCropMaxSquare( const char* name ) {}

WindowWithCropMaxSquare( const string& name ) {}

编译器不会为带有非const的函数赋予常量作为参数,因为这样的函数向您指示它可能会修改给定的参数,显然不允许使用常量。