(错误代码复制并粘贴在底部)
我正在关注在线教程,但是也有3本书,并通过microsoft @ edX完成了高级c ++课程,但是意识到我仍然是一个初学者。我提到这一点是为了让人们认为我主要不是通过差劲的YouTube教程来学习,不是。
此错误来自我创建的c ++窗口类'Window',其中包含另一个类'Windowclass'。它们用于创建窗口,内部的singleton类隐藏细节。
我将从构造函数中复制我认为相关的代码。
似乎问题可能出在窗口构造函数@ hWnd = CreateWindow(这里,名称等,等等:
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,则该程序似乎运行良好。
这是吸气剂的代码,请注意,吸气剂在其他地方似乎工作正常。例如,在创建窗口类名
时调用它wc.lpszClassName = GetName();
字母:
const char* Window::WindowClass::GetName() noexcept {
return wndClassName;
}
虽然我不想复制太多代码,但下面是WindowClass,它位于其父类'Window'中。
[还请注意,我的心理健康状况不佳,编码确实可以帮助我恢复精神健康(中间有很多休息),而争论和侮辱则无济于事。但是很乐意接受建设性的反馈。
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;
}
您在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;
...
我进行了更改之后,崩溃消失了,并且您的窗口出现了: