我想要一些数据缓存,其中包含一些我可以通过UpdateCache
函数更新的对象。但是,我遇到了 aLNK2001
后跟 a 的问题LNK1120
。
HeaderFile.h
#ifndef headerfile_included
#define headerfile_included
#include <vector>
struct MyObject {
unsigned int A;
unsigned int B;
};
class MyClass {
private:
static std::vector<MyObject> myObjectCache;
public:
static void UpdateCache ();
};
#endif
CodeFile.cpp
#include "HeaderFile.h"
void MyClass::UpdateCache () {
myObjectCache.clear();
/* Repopulate cache with new data */
}
我从链接中得到的错误信息是
错误 LNK2001: 无法解析的外部符号 ""private: static class std::vector > MyClass::myObjectCache" (?myObjectCache@MyClass@@0V?$vector@UMyObject@@V?$allocator@UMyObject@@@std@@@标准@@A)"。
致命错误 LNK1120:1 个未解决的外部问题
我的观点是分区到头文件和代码文件有一些问题,因为我在分区不正确时遇到了类似的问题。如果再次出现这样的问题,那么如果您可以发布一些关于将什么放入头文件以及将什么放入代码文件的规则会很好,因为它非常令人困惑。