对于 Windows 应用程序,我试图让 CreateWindow()
和 WndProc()
(或它们的我的版本)成为单例类的一部分在 _tWinMain()
开头创建,但自从尝试将函数转移到 GameHandler.h
和 GameHandler.cpp
后,我一直收到“ Unresolved external 问题”符号 _WinMain@16”。它们最初是 main.cpp
中的全局函数,一切都编译正常,然后我决定将它们移到 GameHandler 中,因为我得到的只是未解析的外部函数,即使我尝试将它们移回 main.cpp
.
我在 VS2010 中执行此操作,该项目是作为 Windows 应用程序创建的,并且没有在属性中设置特定的入口点(我仔细检查过,因为到目前为止我发现的每个解决方案都说这是因为它是一个控制台应用程序 -这不是)。
我目前的代码如下所示。实际项目还有几千行其他代码,我认为它们不相关(但很高兴证明是错误的),但我遗漏了其他几千行代码。虽然实际的窗口创建代码是相关的,但我不认为代码本身是问题(除了我留下的),它是 GameWindowProc() 和/或 CreateGameWindow() 的位置或它们的调用方式。实际的窗口创建代码取自 NeHe's tutorial 。尝试编译以下代码仅给出上述 Unresolved external 。
主要.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
如您所见,我设置了一些预处理器条件来在 main.cpp
中或作为 GameManager 的一部分的麻烦函数之间切换。注释掉 GameManager.h
开头的 #define USEGMGR
,将它们作为 main.cpp
中的全局函数。
有人可以告诉我我做错了什么吗?
编辑: 删除了有关在运行时无法退出的评论。
最佳答案
WinMain
函数不能是类的成员函数,即使是“单例”类。
由于它作为程序的“入口点”,基本上取代了传统 C++ 应用程序中的 main
函数,因此它需要是一个自由的全局函数。
消息 Unresolved external _WinMain@16
只是编译器告诉您它无法找到预期入口点的神秘方式,名为 WinMain
(加上一些名称修改) .
关于c++ - "Unresolved external _WinMain@16"如果我将 WndProc 移动到另一个 cpp 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10445275/