gcnew KeyEventHandler compile problem (VC++)
My managed c++ code fails to compile with the error message
.\Window.cpp(11) : error C2440: 'initializing' : cannot convert from 'System::Windows::Forms::Form ^' to 'Enviroment::Window ^'
No user-defined-conversion operator available, or
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or functi开发者_如何学Goon-style cast
.\Window.cpp(11) : error C3754: delegate constructor: member function 'Enviroment::Window::_keydown' cannot be called on an instance of type 'System::Windows::Forms::Form ^'
Error 1 error C2440: 'initializing' : cannot convert from 'System::Windows::Forms::Form ^' to 'Enviroment::Window ^' c:\Users\Thomas\Documents\Visual Studio 2008\Projects\Project_X\Project_X\Window.cpp 11
Error 2 error C3754: delegate constructor: member function 'Enviroment::Window::_keydown' cannot be called on an instance of type 'System::Windows::Forms::Form ^' c:\Users\Thomas\Documents\Visual Studio 2008\Projects\Project_X\Project_X\Window.cpp 11
In window.h
ref class Window
{
public:
Window();
void _keydown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e);
}
In window.cpp
Window::Window()
{
Form^ form = gcnew Form();
form->KeyDown+= gcnew KeyEventHandler(form, &Window::_keydown);
}
and later
void Window::_keydown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
{
//stuff
}
Help!
I think you mean to say:
form->KeyDown+= gcnew KeyEventHandler(this, &Window::_keydown);
In C++, a class function pointer is comprised of 2 things, the actual pointer (this part you got right) and a pointer to "this" to be passed to the function, which is of the type of the class holding the function. This is your Window
, not Microsoft's Form
.
精彩评论