0

(错误“代码”复制并粘贴在底部)

我正在关注一个在线教程,但也有 3 本书并完成了 microsoft @ edX 的高级 c++ 课程,但意识到我仍然是初学者。我提到这一点是为了让人们不认为我主要是通过糟糕的 youtube 教程学习的,我不是。

此错误来自我创建的包含另一个类“Windowclass”的 c++ Window 类“Window”。它们用于创建窗口,内部单例类隐藏了细节。

我将复制我认为与构造函数相关的代码。

看来问题可能出在窗口构造函数中 @ hWnd = CreateWindow( Here , name, etc 等:

Window::Window(int width, int height, const char* name) noexcept

{
    RECT wr;
    wr.left = 100;
    wr.right = width + wr.left;
    wr.top = 100;
    wr.bottom = height + wr.top;
    AdjustWindowRect(&wr, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, false);
    //create window & get hWnd
    hWnd = CreateWindow(WindowClass::GetName(), name, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | CW_USEDEFAULT | CW_USEDEFAULT, wr.right, wr.left, wr.bottom, wr.top, nullptr, nullptr, WindowClass::GetInstance(), this);

    ShowWindow(hWnd, SW_SHOWDEFAULT);
}

如果我将 CreateWindow 的第一个参数更改为我认为所谓的字符串文字,即“Window”而不是 GetName() getter,那么程序似乎运行良好。

这是 getter 的代码,请注意 getter 在其他地方似乎工作正常。例如,在创建窗口类名时调用它时

wc.lpszClassName = GetName();

吸气剂:

const char* Window::WindowClass::GetName() noexcept {

    return wndClassName;
}

虽然我不想复制太多代码,但下面是位于其父类“Window”中的 WindowClass。

另请注意,我的心理健康状况不佳,编码确实有助于我的心理健康恢复(两者之间有很多休息),争论和侮辱则没有。但乐于接受建设性的反馈。

class WindowClass
{
public:
    static const char* GetName() noexcept;
    static HINSTANCE GetInstance() noexcept;

private:
    WindowClass() noexcept;
    ~WindowClass();
    WindowClass(const WindowClass&) = delete;
    WindowClass& operator=(const WindowClass&) = delete;
    static constexpr const char* wndClassName = "Ryan Direct3d";
    static WindowClass wndClass;
    HINSTANCE hInst;
};

我感谢所有相关的反馈。为什么会出现:WindowsApp1.exe 中的 0x00000000 处引发异常:0xC0000005:执行位置 0x00000000 的访问冲突。WindowsApp1.exe 中 0x7155CCE0 处未处理的异常:0xC000041D:在用户回调期间遇到未处理的异常。

在这一行下面编译的代码


#include <Windows.h>
#include <string>

class Window
{
private:
    //singleton class - manages registration and cleanup / of window  class (unregistered)
    class WindowClass
    {
    public:
        static const char* GetName() noexcept;
        static HINSTANCE GetInstance() noexcept;

    private:
        WindowClass() noexcept;
        ~WindowClass();
        WindowClass(const WindowClass&) = delete;
        WindowClass& operator=(const WindowClass&) = delete;
        //wndClassName called by getter
        static constexpr const char* wndClassName = "Ryan";
        static WindowClass wndClass;
        HINSTANCE hInst;
    };
public:

    Window(int width, int height, const char* name) noexcept;
    ~Window();
    Window(const Window&) = delete;
    Window& operator= (const Window&) = delete;

private:
    int width = 0;
    int height = 0;
    HWND hWnd;
};


//Window Class stuff
Window::WindowClass Window::WindowClass::wndClass; //
Window::WindowClass::WindowClass() noexcept : hInst(GetModuleHandle(nullptr))
{

    WNDCLASSEX wc = { 0 };
    wc.cbSize = sizeof(wc);
    wc.style = CS_OWNDC;
    //wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = GetInstance();
    wc.hIcon = nullptr;
    wc.hCursor = nullptr;
    wc.hbrBackground = nullptr;
    wc.lpszMenuName = nullptr;
    wc.lpszClassName = GetName();
    wc.hIconSm = nullptr;
    RegisterClassEx(&wc);

}


const char* Window::WindowClass::GetName() noexcept {

    return wndClassName;
}

HINSTANCE Window::WindowClass::GetInstance() noexcept {

    return wndClass.hInst;

}
Window::WindowClass::~WindowClass()
{
    UnregisterClass(wndClassName, GetInstance());
}


//Window Creation
Window::Window(int width, int height, const char* name) noexcept
{
    RECT wr;
    wr.left = 100;
    wr.right = width + wr.left;
    wr.top = 100;
    wr.bottom = height + wr.top;
    AdjustWindowRect(&wr, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, false);
    //create window & get hWnd
    hWnd = CreateWindow("Ryan", name, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | CW_USEDEFAULT | CW_USEDEFAULT, wr.right, wr.left, wr.bottom, wr.top, nullptr, nullptr, WindowClass::GetInstance(), nullptr);
        ShowWindow(hWnd, SW_SHOW);
}


Window::~Window() {

    DestroyWindow(hWnd);
}



//wndprocs
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch (msg) //use a switch because in future we will add / process many kinds of messages
    {
    case WM_KEYUP:
        if (wParam == 'F')
        {
            SetWindowText(hWnd, "Dogmah");
        }
        break;

    case WM_CLOSE:
        PostQuitMessage(69);
        break;
    case WM_KEYDOWN:
        if (wParam == 'F')
        {
            SetWindowText(hWnd, "New Name");
        }
        break;
    case WM_CHAR:
    {
        static std::string title;
        title.push_back((char)wParam);
        SetWindowText(hWnd, title.c_str());
    }
    case WM_LBUTTONDOWN:
    {

    }

    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}

int CALLBACK WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR   lpCmdLine,
    int nCmdShow)
{

    Window Ryan(600, 500, "Hey");


    //message pump
    MSG msg;
    BOOL gResult;
    //register custom class for outputting EXTRA msg details

    while ((gResult = GetMessage(&msg, nullptr, 0, 0)) > 0)
    {
        TranslateMessage(&msg);

        DispatchMessage(&msg);
    }

    if (gResult == -1) { return -1; }

    else { return msg.wParam; }

    return 0;
}
4

1 回答 1

0

您在 WNDCLASSEX 中注释掉了 WndProc 分配:

Window::WindowClass::WindowClass() noexcept : hInst(GetModuleHandle(nullptr))
{

    WNDCLASSEX wc = { 0 };
    wc.cbSize = sizeof(wc);
    wc.style = CS_OWNDC;
    //wc.lpfnWndProc = WndProc;
    ...

要修复,只需转发声明您的 WndProc 函数(定义如下)。并取消注释。

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

//Window Class stuff
Window::WindowClass Window::WindowClass::wndClass; //
Window::WindowClass::WindowClass() noexcept : hInst(GetModuleHandle(nullptr))
{

    WNDCLASSEX wc = { 0 };
    wc.cbSize = sizeof(wc);
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    ...

在我做出改变后,崩溃消失了,你的窗口出现了:

在此处输入图像描述

于 2020-03-19T21:59:21.060 回答