简体   繁体   中英

Pure Virtual Method Called terminate

I am following a tutorial on MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/ff381400(v=vs.85).aspx

Which shows how to make a class to create windows in WINAPI.

I did the exact same thing as them, except that I put the Creation code in the constructor.

When ran, it gives pure virtual method called error and terminates the program with no explanaton.

template<typename T>
class Form
{
    public:
        Form(std::string Title, std::string Class, /*All other params*/);

    private:
        static LRESULT __stdcall WindowProcedure(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam);

    protected:
        HWND WindowHandle = nullptr;
        virtual LRESULT HandleMessages(UINT Msg, WPARAM wParam, LPARAM lParam) = 0;
};



template<typename T>
Form<T>::Form(std::string Title, std::string Class, /*All Other Params*/)
{
    WNDCLASSEX WndClass = {/*Filled in with other params*/};

    RegisterClassEx(&WndClass);
    WindowHandle = CreateWindowEx(/*Other params*/, this);
}

template<typename T>
LRESULT __stdcall Form<T>::WindowProcedure(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    T* Data = nullptr;

    switch (Msg)
    {
        case WM_NCCREATE:
        {
            CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
            Data = reinterpret_cast<T*>(pCreate->lpCreateParams);
            SetWindowLongPtr(Hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(Data));
            break;
        }

        default:
        {
            Data = reinterpret_cast<T*>(GetWindowLongPtr(Hwnd, GWLP_USERDATA));
            return Data ? Data->HandleMessages(Msg, wParam, lParam) : DefWindowProc(Hwnd, Msg, wParam, lParam);
        }
    }
    return 0;
}

Then my main window:

class MainWindow : public Form<MainWindow>
{
    public:
        MainWindow(std::string Title, /*Other params*/);
        LRESULT HandleMessages(UINT Msg, WPARAM wParam, LPARAM lParam);

        HWND WindowHandle() {return Form::WindowHandle;}
};

MainWindow::MainWindow(std::string Title, std::string Class, /*Other params*/) {}

LRESULT MainWindow::HandleMessages(UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
        case WM_CREATE:
        {
        }
        break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(WindowHandle(), Msg, wParam, lParam);
    }

    return 0;
}

That is all. Why does it give me that error and how can I fix it?

I did the exact same thing as them, except that I put the Creation code in the constructor.

This is why. Calling a virtual method from the constructor doesn't do what you expect - unless you expect it to call the method from the current class in the hierarchy. When a base class constructor executes, the object isn't of derived type yet. And if you call a virtual method which is pure in the current class from the constructor of that class, you'll get that error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM