c++ - 使用静态成员声明类时遇到问题
问题描述
所以基本上我正在尝试创建一个类和一个(静态)函数来访问它,但由于某种原因,代码拒绝编译并抛出 2 个“未解析的外部符号”错误。
// game.cpp
#include "game.h"
CGame::CGame()
{
// ...
}
CGame* CGame::getInstance()
{
if (s_instance == nullptr)
s_instance = new CGame();
return s_instance;
}
void CGame::run()
{
// ...
}
...
// main.cpp
#include "game.h"
int main()
{
CGame::getInstance()->run();
}
...
#ifndef _GAME_
#define _GAME_
class CGame
{
private:
static CGame* s_instance;
public:
static CGame* getInstance();
void run(); // For testing purposes
CGame();
};
#endif _GAME_
这里有什么问题,我怎么可能创建一个我 [可以] 使用而不声明它的类?
解决方案
CGame* CGame::s_instance = nullptr;
您的 cpp 中缺少。
推荐阅读
- python - 如何在python-pptx中获取给定形状的连接点坐标
- python - 多列中的熊猫 groupby 和 countif
- windows - 如何在 Visual Studio 的“附加清单依赖项”链接器选项中指定“文件”元素
- java - Java Selenium 如何“将图像另存为”并按 Enter
- spring - 没有指定 CacheResolver,也没有找到 CacheManager 类型的唯一 bean
- flask - 如何使用 gunicorn 在 GCP 上托管烧瓶应用程序?
- reactjs - 获取数据时reactjs视图搞砸了
- html - 来自 mockAPI 的数据未在 Angular 8 UI 中正确显示
- sql - 在 SQL 中将多列合并为一列
- reactjs - React Hook Form中具有共享名称的不同输入