2013-08-21 42 views
0

我一整晚都在用這個東西拍我的腦袋。我正在使用Windows應用程序,無論出於何種原因,我無法在堆棧上創建std::list的實例。它導致CreateWindow()失敗,並沒有告訴我任何有用的東西。無法在堆棧上創建std :: list

我的窗口代碼是非常標準的,沒有什麼不尋常的,除了我的程序的一些東西。

#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也可以正常工作。如果我無法解決這個問題,我可能會最終使用它。有沒有人見過這樣的事情?

+0

當例如'CreateWindow'失敗,爲什麼不使用'GetLastError'來獲取錯誤代碼?它會告訴你爲什麼失敗。 –

+0

另外,自從創建了CGameProcessor實例(*在堆上*而不是堆棧)以來,我不明白'CreateWindow'會如何失敗,如果列表在'CGameProcessor'類中在**之後你可以調用'CreateWindow'。 –

+0

這不是問題,但是以下劃線開頭的名稱後跟一個大寫字母('_GAMEPROCESSOR_H_')和包含兩個連續下劃線的名稱將保留給實施。不要使用它們。 –

回答

3

這是一個錯誤:

HWND hWnd = CreateWindow(
    (LPCWSTR) "mainWin" 

,因爲它是鑄造char字符串文字到wchar_t*。在其他地方使用_T()宏或使用寬字符串文字(L"mainWin")。在分配wcex.lpszClassName時,在代碼的前面也有一個相同的錯誤。如果CreateWindow()未能使用GetLastError()來確定失敗的原因,

+0

我會做出這些改變,看看我可以從那裏去。謝謝! – MGZero

+0

就是這樣。注意自我,不要相信每個教程的窗口創建。 – MGZero