1

我的托管 c++ 代码无法编译并显示错误消息

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

帮助!

4

1 回答 1

2

我想你的意思是:

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

在 C++ 中,类函数指针由两部分组成,实际指针(这部分你做对了)和指向要传递给函数的“this”的指针,它是持有函数的类的类型。这是你的Window,不是微软的Form

于 2010-01-03T00:57:19.063 回答