c ++从实现文件访问私有静态成员

c++ access private static member from implementation file

我有这样一个头文件

#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]){
            ...
        }
    }
}

当我试图编译它时,它给我一个错误 class "MyClass" 没有成员 "SomeMap"。我该如何解决这个问题?

您忘记定义静态变量:

#include "Header.h"
#include <map>
namespace MyApp{
    std::map<int, bool> MyClass::SomeMap;

    void MyClass::DoSomething(int arg){
        if(MyClass::SomeMap[5]){
            ...
        }
    }
}

P.S。 class 定义后,您的示例代码缺少 ;