wxWidgets 拖放文件事件处理程序初始化问题(无效static_cast)
wxWidgets Drop Files eventhandler initialization problem (invalid static_cast)
我正在尝试制作一个简单的程序,使用带有(wxWidgets(wxListCtrl的C++将文件拖放到列表中。
- 我使用的工具:代码::块20.03,MinGW 17.1,wxWidgets 3.1.3,wxFormBuilder 3.9.0。
- 操作系统:视窗 10 专业版。
我尝试了以下内容:(在DnD_SimpleFrame类构造函数中调用的函数 SetupGUI(( 中。
m_listCtrl1 = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_NO_HEADER|wxLC_REPORT );
m_listCtrl1->DragAcceptFiles(true);
m_listCtrl1->Connect(wxEVT_DROP_FILES, wxDropFilesEventHandler(DnD_SimpleFrame::OnDropFiles), NULL, this);
删除文件(从资源管理器(时要调用的函数是:
bool DnD_SimpleFrame::OnDropFiles(wxArrayString &filenames)
{
size_t nFiles = filenames.GetCount();
wxString str;
str.Printf( "%d files dropped", (int)nFiles);
m_listCtrl1->DeleteAllItems();
if (m_listCtrl1 != NULL)
{
m_listCtrl1->InsertItem(0, str);
for ( size_t n = 1; n < (nFiles+1); n++ )
m_listCtrl1->InsertItem(n, filenames[n]);
}
return true;
}
m_listCtrl1->Connect(...(; 行上的生成消息是:
||=== Build: Debug in DnD_Simple (compiler: GNU GCC Compiler) ===|
F:Data__C++wxAppsDnD_SimpleDnD_SimpleMain.cpp||In member function 'void DnD_SimpleFrame::SetupGUI()':|
F:SDKswx313includewxevent.h|149|error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}|
F:SDKswx313includewxevent.h|4196|note: in expansion of macro 'wxEVENT_HANDLER_CAST'|
F:Data__C++wxAppsDnD_SimpleDnD_SimpleMain.cpp|92|note: in expansion of macro 'wxDropFilesEventHandler'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
我在这里做错了什么(或忘记了(?
如果您仔细查看错误消息:
error: invalid static_cast from type 'bool (DnD_SimpleFrame::*)(wxArrayString&)' to type 'wxDropFilesEventFunction' {aka 'void (wxEvtHandler::*)(wxDropFilesEvent&)'}
你可以看到它说它不能将某些东西从一种类型转换为另一种类型。
如果您不熟悉C++,则可能难以分析这些类型,但您应该能够看到这些是函数指针(实际上是指向类成员的指针(。查看您的代码,您还应该能够看到第一个是您的DnD_SimpleFrame::OnDropFiles
函数的类型,因此问题是它无法转换为预期的函数类型。
最后,这样做的原因只是你的函数没有正确的参数类型:你应该给wxWidgets一些需要wxDropFilesEvent&
的东西,但你的函数需要wxArrayString&
。您必须更改它以获取wxDropFilesEvent& event
然后使用事件对象GetFiles()
方法来获取实际文件。
在完全不同的主题上,您应该在新代码中使用Bind()
而不是Connect()
。如果您遵循使用后者的教程,则表明它已经过时了。您的代码仍然不会使用Bind()
进行编译,但错误消息会稍微简单一些。
相关文章:
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 无法访问嵌套类.类的使用无效
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 如何解决错误:SCIP C++中的 SCIP 阶段无效 <10>
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- FFMPEG配置文件级别id大小无效
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 错误:从"int"到枚举c++的转换无效
- 如何修复此错误:className::className的无效使用
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- C++-模板嵌套类的引用初始化无效
- 错误:无效的预处理指令 #i 的意思是 #if?
- 多维数组 C++ 中数组下标的类型"int[int]"无效
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 从 'int' 到 'int*' CPP 的转换无效
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息
- C++ PTHREADS - 无效转换无效*(*)()到无效*(*)(无效*)
- 声明为无效的变量或字段'...' Ardunio 编译器上的错误