如何正确通过构造函数创建wxPopupTransientWindow?(wxWidgets/C++)
wxPopupTransientWindow 实例化问题及解决
我想创建一个弹出窗口,但网上的示例都是自定义wxPopupTransientWindow类,没法像创建wxPanel这类组件那样直接用构造函数实例化(比如wxPanel* P = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(50, 50), 0);)。
当前代码
wxPopupTransientWindow* PopupWindow = new wxPopupTransientWindow(this, wxBORDER_NONE); PopupWindow->SetSize(50, 50); PopupWindow->SetPosition(wxDefaultPosition);
编译错误信息
1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(59,1): error C2201: 'const `public: __cdecl MyFrame::MyFrame(wxString const &,wxPoint const &,wxSize const &)'::`2'::wxPopupWindowBase::`vftable'': 必须具有外部链接才能导出/导入 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(38,14): error C3640: 'MyFrame::{ctor}::wxPopupWindowBase::~wxPopupWindowBase': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(53,18): error C3640: 'MyFrame::{ctor}::wxPopupWindowBase::Position': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\msw\popupwin.h(52,5): error C2071: 'MyFrame::{ctor}::wxPopupWindow::ms_classInfo': 非法存储类 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\msw\popupwin.h(52,5): error C2246: 'MyFrame::{ctor}::wxPopupWindow::ms_classInfo': 局部定义的类中存在非法静态数据成员 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\msw\popupwin.h(53,1): error C2201: 'const `public: __cdecl MyFrame::MyFrame(wxString const &,wxPoint const &,wxSize const &)'::`2'::wxPopupWindow::`vftable'': 必须具有外部链接才能导出/导入 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\msw\popupwin.h(28,14): error C3640: 'MyFrame::{ctor}::wxPopupWindow::~wxPopupWindow': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\msw\popupwin.h(30,18): error C3640: 'MyFrame::{ctor}::wxPopupWindow::SetFocus': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\msw\popupwin.h(31,18): error C3640: 'MyFrame::{ctor}::wxPopupWindow::Show': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\msw\popupwin.h(34,21): error C3640: 'MyFrame::{ctor}::wxPopupWindow::MSWGetStyle': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\msw\popupwin.h(37,20): error C3640: 'MyFrame::{ctor}::wxPopupWindow::MSWGetParent': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\msw\popupwin.h(52,5): error C3640: 'MyFrame::{ctor}::wxPopupWindow::GetClassInfo': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(125,1): error C2201: 'const `public: __cdecl MyFrame::MyFrame(wxString const &,wxPoint const &,wxSize const &)'::`2'::wxPopupTransientWindowBase::`vftable'': 必须具有外部链接才能导出/导入 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(112,18): error C3640: 'MyFrame::{ctor}::wxPopupTransientWindowBase::Destroy': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(153,5): error C2071: 'MyFrame::{ctor}::wxPopupTransientWindow::ms_classInfo': 非法存储类 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(153,5): error C2246: 'MyFrame::{ctor}::wxPopupTransientWindow::ms_classInfo': 局部定义的类中存在非法静态数据成员 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(155,1): error C2201: 'const `public: __cdecl MyFrame::MyFrame(wxString const &,wxPoint const &,wxSize const &)'::`2'::wxPopupTransientWindow::`vftable'': 必须具有外部链接才能导出/导入 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(138,18): error C3640: 'MyFrame::{ctor}::wxPopupTransientWindow::Popup': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(139,18): error C3640: 'MyFrame::{ctor}::wxPopupTransientWindow::Dismiss': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(142,18): error C3640: 'MyFrame::{ctor}::wxPopupTransientWindow::MSWHandleMessage': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(148,18): error C3640: 'MyFrame::{ctor}::wxPopupTransientWindow::MSWDismissUnfocusedPopup': 局部类的引用或虚成员函数必须定义 1>C:\Users\husse\C++\Data-Mange-Bot-C++ver\DataManageApp\vcpkg\installed\x64-windows\include\wx\popupwin.h(153,5): error C3640: 'MyFrame::{ctor}::wxPopupTransientWindow::GetClassInfo': 局部类的引用或虚成员函数必须定义
解决方法
wxPopupTransientWindow是抽象基类,无法直接实例化,必须自定义子类实现必要的虚函数,同时注意不要将自定义类放在函数内部(避免局部类相关错误):
1. 定义自定义弹出窗口类
// 放在全局作用域或命名空间下,不要放在函数内部 class MyPopupWindow : public wxPopupTransientWindow { public: MyPopupWindow(wxWindow* parent, int style = wxBORDER_NONE) : wxPopupTransientWindow(parent, style) {} // 重写必要的虚函数,直接调用基类实现即可满足基础需求 void Popup(wxWindow* focus = nullptr) override { wxPopupTransientWindow::Popup(focus); } void Dismiss() override { wxPopupTransientWindow::Dismiss(); } };
2. 使用自定义类实例化并显示
MyPopupWindow* PopupWindow = new MyPopupWindow(this, wxBORDER_NONE); PopupWindow->SetSize(50, 50); PopupWindow->SetPosition(wxDefaultPosition); PopupWindow->Popup(); // 调用Popup方法显示弹出窗口
内容的提问来源于stack exchange,提问作者Pal
相关产品推荐
相关产品推荐

