English 中文(简体)
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 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:UsersThomasDocumentsVisual Studio 2008ProjectsProject_XProject_XWindow.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:UsersThomasDocumentsVisual Studio 2008ProjectsProject_XProject_XWindow.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.

问题回答

暂无回答




相关问题
how to reliable capture display setting changed

static void Main() { // Set the SystemEvents class to receive event notification when a user // when display settings change. SystemEvents.DisplaySettingsChanged += new ...

Why use CComBSTR instead of just passing a WCHAR*?

I m new to COM. What exactly is the advantage of replacing: L"String" with CComBSTR(L"String") I can see a changelist in the COM part of my .NET application where all strings are replaced in this ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

Statically linking Winsock?

I m using Winsock 1.1 in my project. I include wsock32.lib in "Additional Dependencies". I m looking at the DLL project using depends.exe and notice that the DLL depends on wsock32.dll. How can I ...

热门标签