gcnew KeyEventHandler编译问题(VC++)

发布于 2024-08-16 19:24:14 字数 1552 浏览 11 评论 0原文

我的托管 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

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);
}

及更高版本

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

帮助!

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 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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-08-23 19:24:14

我想你的意思是:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文