2010-01-03 60 views
1

我的託管C++代碼失敗,該錯誤消息gcnew KeyEventHandler編譯問題(VC++)

.\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 function-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 

在window.h中編譯

ref class Window 
    { 
    public: 
     Window(); 
     void _keydown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e); 
    } 

在window.cpp

Window::Window() 
    { 
     Form^ form = gcnew Form(); 
     form->KeyDown+= gcnew KeyEventHandler(form, &Window::_keydown); 
} 

及以後

void Window::_keydown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) 
    { 
     //stuff 
    } 

幫助!

回答

2

我想你的意思是說:

form->KeyDown+= gcnew KeyEventHandler(this, &Window::_keydown); 

在C++中,一個類的函數指針由兩件事情,實際指針(這部分你有右)和一個指向「本」是傳遞給函數,它是持有該函數的類的類型。這是你的Window,而不是微軟的Form

+0

現貨:)乾杯! – gingerbreadboy 2010-01-03 01:06:17