gpt4 book ai didi

c++ - C/C++ - 从封闭类继承的嵌套类

转载 作者:行者123 更新时间:2023-11-30 03:04:21 25 4
gpt4 key购买 nike

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

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 函数。

在主要我有

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

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

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

那么,谁能告诉我如何解决这个问题?

最佳答案

您需要两件事:每个构造函数的主体,以及正确的 const'ness。

WindowWithCropMaxSquare( char* name );

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

WindowWithCropMaxSquare( char* name ) {}

我也很怀疑

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

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

WindowWithCropMaxSquare( const char* name ) {}

WindowWithCropMaxSquare( const string& name ) {}

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

关于c++ - C/C++ - 从封闭类继承的嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8676834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com