3

对于 Windows 应用程序,我正在尝试获取CreateWindow()并且WndProc()(或我的版本)成为在开始时创建的单例类的一部分,_tWinMain()但由于尝试将函数转移到GameHandler.h并且GameHandler.cpp我不断收到“未解析的外部符号 _WinMain@ 16"。它们最初是全局函数main.cpp,一切都编译得很好,然后我决定将它们移到 GameHandler ,因为我得到的只是未解决的外部函数,即使我尝试将它们移回main.cpp.

我在 VS2010 中这样做,该项目是作为 Windows 应用程序创建的,并且在属性中没有设置特定的入口点(我仔细检查了到目前为止我发现的每个解决方案都说这是因为它是一个控制台应用程序 - 这不是t)。

我目前拥有的代码如下所示。实际项目有几千行其他代码,我认为它不相关(但很高兴证明是错误的。虽然实际的窗口创建代码是相关的,但我认为代码本身并不相关)是问题所在(除了我留下的),它是 GameWindowProc() 和/或 CreateGameWindow() 的位置或它们的调用方式。实际的窗口创建代码取自NeHe 的教程。尝试仅编译以下代码给出上述未解决的外部。

主.cpp:

#include <Windows.h>
#include "GameManager.h"

#ifndef USEGMGR
bool CreateGameWindow(char* title, int width, int height, int bits, bool fullScreenFlag);
LRESULT CALLBACK GameWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
#endif

int APIENTRY _tWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, 
                        LPTSTR lpCmdLine, int nCmdShow)
{
    GameManager::Startup();
    GameManager* GMgr = GameManager::GetInstance();

    GMgr->SetProgramState(GAME_MODE);
    while(GMgr->GetProgramState() != GAME_MODE) // Normally this would be if (State != GAME_QUIT)
    { /* do game related stuff */ }

    GameManager::Shutdown();
    return 0;
}

#ifndef USEGMGR
bool CreateGameWindow(char* title, int width, int height, int bits, bool fullScreenFlag)
{
    // Fairly complex but flexible creation code, taken from NeHe's tutorials. Of relevant interest is:
    WNDCLASS        wc;                             // Windows Class Structure
    wc.lpfnWndProc  = (WNDPROC) GameWindowProc;  // WndProc Handles Messages
    if (!RegisterClass(&wc))                         // Attempt To Register The Window Class
    {
        MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return false;
    }
    return true;
}

LRESULT CALLBACK GameWindowProc(HWND    hWnd,            // Handle For This Window
    UINT    uMsg,            // Message For This Window
    WPARAM    wParam,            // Additional Message Information
    LPARAM    lParam)            // Additional Message Information
{
    // various custom message handling, if not processed:
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
#endif

在 GameManager.h 中:

#ifndef GAMEMANAGER_H
#define GAMEMANAGER_H
#define USEGMGR // makes CreateGameWindow() and GameWindowProc() methods in GameManager instead of global

#include <Windows.h>

enum ProgramState
{
    GAME_MODE,
    GAME_QUIT,
};

class GameManager
{
public:
    static void             Startup();
    static void             Shutdown();
    static GameManager*     GetInstance();
    void                    Update(); // code not shown, check quit key etc
#ifdef USEGMGR
    const bool              CreateGameWindow(char* title, int width, int height, int bits, bool fullScreenFlag);
    static LRESULT CALLBACK GameWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
#endif
    void                    KillGameWindow(void);
    const int                GetProgramState() const;
    void                    SetProgramState(const int& newMode);
private:
    GameManager();
    ~GameManager();
    GameManager(const GameManager&);
    GameManager& operator=(const GameManager&);
    HINSTANCE                m_hInstance;
    HWND                    m_hWnd;        
    HDC                        m_hDC;        
    static GameManager*        s_instance;
    int                        m_programState; // uses ProgramState enum
};
#endif

在 GameManager.cpp 中:

#include "GameManager.h"
#include <Windows.h>
#include <assert.h>

#ifndef USEGMGR
extern bool CreateGameWindow(char* title, int width, int height, int bits, bool fullScreenFlag);
#endif
GameManager*        GameManager::s_instance    = NULL;
GameManager::GameManager(){}
GameManager::~GameManager(){}


void GameManager::Startup()
{
    assert(s_instance == NULL);
    s_instance = new GameManager;
#ifdef USEGMGR
    if (! (s_instance->CreateGameWindow("Game Window", 800, 600, 32, true )) )
#else
    if (! (CreateGameWindow("Game Window", 800, 600, 32, true )) )
#endif
        assert("CreateGameWindow failed! Need an error here"); // Quit If Window Was Not Created - clean this up later    
}

void GameManager::Shutdown()
{
    assert(s_instance != NULL);
    delete s_instance;
    s_instance = NULL;
}

GameManager* GameManager::GetInstance(){return s_instance;}

void GameManager::Update(){/* msg handling, watch for quit key, etc */}
const int GameManager::GetProgramState() const{return s_instance->m_programState;}
void GameManager::SetProgramState(const int& newState){s_instance->m_programState = newState;}

#ifdef USEGMGR
const bool GameManager::CreateGameWindow(char* title, int width, int height, int bits, bool fullScreenFlag)
{
    // Fairly complex but flexible creation code, taken from NeHe's tutorials. Of relevant interest is:
    WNDCLASS        wc;                             // Windows Class Structure
    wc.lpfnWndProc  = (WNDPROC) GameManager::GameWindowProc;  // WndProc Handles Messages
    if (!RegisterClass(&wc))                         // Attempt To Register The Window Class
    {
        MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return false;
    }
    return true;
}

LRESULT CALLBACK GameManager::GameWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    // various custom message handling, if not processed:
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
#endif    

正如你所看到的,我已经设置了一些预处理器条件来在 GameManager 中main.cpp或作为 GameManager 的一部分的麻烦函数之间切换。#define USEGMGR在开头注释掉以GameManager.h将它们作为全局函数在main.cpp.

有人可以告诉我我做错了什么吗?

编辑:删除了关于如果你让它运行就无法退出的评论。

4

2 回答 2

5

WinMain函数不能是类的成员函数,即使是“单例”类。

由于它充当程序的“入口点”,基本上取代了main您在传统 C++ 应用程序中找到的函数,因此它需要是一个免费的全局函数。

该消息Unresolved external _WinMain@16只是编译器告诉您它无法找到预期的入口点的神秘方式,命名WinMain(加上一些名称修改)。

于 2012-05-04T08:27:48.387 回答
2

添加

#include <tchar.h>

到顶部,main.cpp以便_tWinMain正确定义宏。

如果您没有宏定义,会发生什么情况,您最终会在目标文件中得到一个名为_tWinMain()(或名称的一些修改版本,如?_tWinMain@@YGHPAUHINSTANCE__@@0PADH@Z())的函数,但链接器和运行时初始化代码正在寻找WinMain()or wWinMain()。他们没有找到。

<tchar.h>定义了一个宏,该宏将名称_tWinMain() 转换为其他所有名称都在寻找的两个名称之一。在开始重构之前(很可能是间接的),您一定已经拥有包含该标头的东西,并且以某种方式丢失了它。

或者您可以省去宏版本并将函数命名为WinMainor wWinMain(无论您是否为 UNICODE 构建,任何一个都应该工作)。如果您这样做,只需记住更改LPTSTR参数声明以匹配您选择的那个。

于 2012-05-04T08:31:40.563 回答