c++ - static CArray C++ MFC
问题描述
I simply cannot seem to be able to make a static CArray and work with it.
Here's my code:
class WhiteBoard
{
public:
static CArray<WhiteBoard, WhiteBoard> test;
void tester()
{
test.Add(*this);
}
};
And upon calling the tester method, I get an unresolved external symbol. Full error code is:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: static class CArray<class WhiteBoard,class WhiteBoard> WhiteBoard::test" (?test@WhiteBoard@@2V?$CArray@VWhiteBoard@@V1@@@A) MFCApplication2 C:\Users\sw.eng\source\repos\MFCApplication2\MFCApplication2Dlg.obj 1
This is my first time working with CArray but I messed around with CArray of int and all worked alright.
Could it be that I'm trying to have a CArray of WhiteBoard inside of WhiteBoard? (I do need this functionality to keep track of all child objects made, which is why I do it)
Removing the static
keyword makes everything run. But then it's not a static member, and I need it to be one.
Anyways, all help would be incredible and very much appreciated.
EDIT 1:
I've made another class which I called BoardBoss. WhiteBoard inherits BoardBoss. CArray is now of BoardBoss type. CArray<BoardBoss, BoardBoss> test;
The issue persists.
解决方案
Static class members need to be defined. As posted, the WhiteBoard
class merely declares the identifier test
. That makes the compiler happy, but the linker fails, because it cannot find the referenced symbol when it is used.
To fix this you need to add the following to a compilation unit:
CArray<WhiteBoard, WhiteBoard> Whiteboard::test;
推荐阅读
- docker - 在 Windows Server 2019 中的 Swarm 上的节点之间批量共享数据
- android - 如何从数据库文件生成 SQLite 实体关系图
- exoplayer2.x - Exoplayer 图像自定义渲染器
- python - Python matplotlib - 如何向垂直线添加注释
- java - 无法让 Jsoup 使用代理:System.setProperty 不起作用
- php - 如何让当前用户进入我的实体类?
- keras - 使用 keras 预处理的图像加载
- python - 从文件夹 python 导入类
- python - 如果启用了张量相等,则张量是不可散列的。相反,使用 tensor.experimental_ref() 作为键
- excel - 如何在 VBA 中将以下 excel 函数重新创建为自定义函数:=IF(ISBLANK(I2), "late", IF(I2 > H2, "late", "on time"))