繁体   English   中英

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

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

您忘记定义静态变量了:

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

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

PS您的示例代码丢失了; 在类定义之后。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM