C++从实现文件访问私有静态成员
我有一个这样的头文件C++从实现文件访问私有静态成员,c++,implementation,static-variables,C++,Implementation,Static Variables,我有一个这样的头文件 #ifndef MYAPP #define MYAPP #include <map> namespace MyApp{ class MyClass{ private: static std::map<int, bool> SomeMap; public: static void DoSomething(int arg); }; } #endif MYAPP
#ifndef MYAPP
#define MYAPP
#include <map>
namespace MyApp{
class MyClass{
private:
static std::map<int, bool> SomeMap;
public:
static void DoSomething(int arg);
};
}
#endif MYAPP
和一个实现文件
#include "Header.h"
#include <map>
namespace MyApp{
void MyClass::DoSomething(int arg){
if(MyClass::SomeMap[5]){
...
}
}
}
当我试图编译它时,它给了我一个错误类MyClass没有成员SomeMap。如何解决此问题?您忘记定义静态变量:
#include "Header.h"
#include <map>
namespace MyApp{
std::map<int, bool> MyClass::SomeMap;
void MyClass::DoSomething(int arg){
if(MyClass::SomeMap[5]){
...
}
}
}
注意:您的示例代码缺失;在类定义之后。可能重复的