我有这样的头文件

#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

和一个实现文件

当我尝试编译它时,它给我一个错误类“ MyClass”,没有成员“ SomeMap”。我怎样才能解决这个问题?

最佳答案

您忘记定义静态变量了:

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

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


附言类定义后,示例代码缺少;

关于c++ - c++从实现文件访问私有(private)静态成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47826675/

10-13 22:50