【问题标题】:c++ access private static member from implementation filec ++从实现文件访问私有静态成员
【发布时间】:2017-12-15 06:04:09
【问题描述】:

我有一个这样的头文件

#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" has no member "SomeMap"。我该如何解决这个问题?

【问题讨论】:

标签: c++ implementation static-variables


【解决方案1】:

您忘记定义静态变量:

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

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

附:您的示例代码在类定义后缺少 ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多