【发布时间】:2013-08-23 10:11:27
【问题描述】:
我整晚都在为这个而绞尽脑汁。我正在使用 Windows 应用程序,无论出于何种原因,我都无法在堆栈上创建 std::list 的实例。它会导致 CreateWindow() 失败,并且没有告诉我任何有用的信息。
我的 Window 代码非常标准,除了我的程序的一些东西之外没有什么特别之处。
#include <windows.h>
#include <string.h>
#include <tchar.h>
#include <sstream>
#include "GameProcessor.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName =(LPCWSTR) "mainWin";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
static TCHAR szWindowClass[] = _T("win32app");
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
(LPCWSTR) "mainWin",
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
WINDOW_WIDTH, WINDOW_HEIGHT,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
/*===========================================
initialize game stuff here
=============================================*/
OIS::ParamList pl;
std::ostringstream wnd;
wnd << (size_t)hWnd;
pl.insert(std::make_pair( std::string("WINDOW"), wnd.str() ));
CGameProcessor * gameProcessor = new CGameProcessor(pl);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
gameProcessor->run();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/*=============================================
CLEAN THAT SHIT UP
===============================================*/
delete gameProcessor;
gameProcessor = nullptr;
return (int) msg.wParam;
}
这是我尝试创建std::list的地方
#ifndef _GAMEPROCESSOR_H_
#define _GAMEPROCESSOR_H_
#include <fstream>
#include <iostream>
#include <map>
#include "EventProcessor.h"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define ENTITYCFG "entities\\config.cfg"
class CGameProcessor
{
public:
void run();
CGameProcessor(OIS::ParamList pl);
private:
//no implementation -- do not use
CGameProcessor(const CGameProcessor &);
CGameProcessor & operator= (const CGameProcessor &);
static CGameProcessor * _singleton;
CEventProcessor * _eventProcessor;
std::list<int> _foo;
};
#endif
我试过把它移到其他类,它似乎在我放的任何地方都爆炸了。如果我改用std::list<int> *,并在ctor中分配它,就没有问题。我真的不想为此使用指针,这很愚蠢。 std::vector 也可以正常工作。如果我无法解决这个问题,我可能最终会使用它。有没有人见过这样的事情?
【问题讨论】:
-
当例如
CreateWindow失败,为什么不使用GetLastError来获取错误代码?它会告诉你失败的原因。 -
另外,如果列表在您的
CGameProcessor类中,我看不出CreateWindow会如何失败,因为创建了CGameProcessor实例(在堆而不是堆栈)之后你调用CreateWindow。 -
这不是问题,但是以下划线开头后跟大写字母 (
_GAMEPROCESSOR_H_) 的名称和包含两个连续下划线的名称保留给实现。不要使用它们。 -
@JoachimPileborg 相信我,我完全明白你在说什么!但是一旦我从该类中删除 std::list ,它就可以正常工作了!
标签: c++ winapi stdlist createwindow