【问题标题】:Cannot create std::list on the stack无法在堆栈上创建 std::list
【发布时间】: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&lt;int&gt; *,并在ctor中分配它,就没有问题。我真的不想为此使用指针,这很愚蠢。 std::vector 也可以正常工作。如果我无法解决这个问题,我可能最终会使用它。有没有人见过这样的事情?

【问题讨论】:

  • 当例如CreateWindow 失败,为什么不使用GetLastError 来获取错误代码?它会告诉你失败的原因。
  • 另外,如果列表在您的CGameProcessor 类中,我看不出CreateWindow 会如何失败,因为创建了CGameProcessor 实例(在堆而不是堆栈)之后你调用CreateWindow
  • 这不是问题,但是以下划线开头后跟大写字母 (_GAMEPROCESSOR_H_) 的名称和包含两个连续下划线的名称保留给实现。不要使用它们。
  • @JoachimPileborg 相信我,我完全明白你在说什么!但是一旦我从该类中删除 std::list ,它就可以正常工作了!

标签: c++ winapi stdlist createwindow


【解决方案1】:

这是一个错误:

HWND hWnd = CreateWindow(
    (LPCWSTR) "mainWin"

因为它将char 字符串文字转换为wchar_t*。像其他地方一样使用_T() 宏或使用宽字符串文字(L"mainWin")。分配给wcex.lpszClassName 时,代码前面也存在相同的错误。

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

【讨论】:

  • 我会做出这些改变,看看我能从那里去哪里。谢谢!
  • 就是这样。提醒自己,不要相信每个创建窗口的教程。
猜你喜欢
  • 2016-05-20
  • 2019-12-13
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2010-12-08
相关资源
最近更新 更多