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

发布于 2024-12-23 05:39:07 字数 1891 浏览 2 评论 0原文

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

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:函数 _main c:\Users\ 中引用的无法解析的外部符号“public: __thiscall Window::WindowWithCropMaxSquare::WindowWithCropMaxSquare(char *)”(??0WindowWithCropMaxSquare@Window@@QAE@PAD@Z)尼古拉斯\文档\视觉工作室2010\项目\AFRTProject\AFRTProject\AFRTProject.obj

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2024-12-30 05:39:07

您需要两件事:每个构造函数的主体,以及正确的常量性。

WindowWithCropMaxSquare( char* name );

只是一个没有任何定义(主体)的声明。正如您在评论中所暗示的那样,空的构造函数主体

WindowWithCropMaxSquare( char* name ) {}

也是我非常怀疑

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

需要一个采用 const char* 的构造函数,因为您给它一个常量(右值):

WindowWithCropMaxSquare( const char* name ) {}

或者

WindowWithCropMaxSquare( const string& name ) {}

编译器将不要将常量作为参数提供给采用非常量的函数,因为此类函数向您表明它可以修改给定的参数,显然不允许使用常量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文