0

我整晚都在为这个而绞尽脑汁。我正在使用 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也可以正常工作。如果我无法解决这个问题,我可能最终会使用它。有没有人见过这样的事情?

4

1 回答 1

3

这是一个错误:

HWND hWnd = CreateWindow(
    (LPCWSTR) "mainWin"

因为它将char字符串文字转换为wchar_t*. 像其他地方一样使用_T()宏或使用宽字符串文字 ( L"mainWin")。分配给wcex.lpszClassName.

如果CreateWindow()失败GetLastError(),则用于确定失败的原因。

于 2013-08-21T07:33:14.170 回答