尝试初始化静态方法"method is private within this context"出错
error "method is private within this context" trying to initialize static method
我有这个类(在头文件中声明(:
class HttpError
{
static std::map<unsigned long, const char *> statusCodes;
std::string msg;
public:
HttpError();
};
我已经把它放在相应的*.cpp文件中:
HttpError::statusCodes = {
{100L, "Continue" },
{101L, "Switching Protocols" }}
当我编译时,我得到了错误";statusCode在此上下文中是私有的";。是的,我知道它是私有的,但它应该如何初始化静态私有成员??
我读过这里和这里的答案,我谦虚地认为我也有同样的答案,但毫无疑问是行不通的。我做错了什么?
我忘记声明正在初始化的变量的类型,即:
std::map<unsigned long, const char *> HttpError::statusCodes = {
{100L, "Continue" },
{101L, "Switching Protocols" }};
当我这么做的时候,错误就解决了。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 为什么使用 "this" 指针调用派生成员函数?
- C++错误:"error: int aaa::bbb is protected within this context"
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 关于C++中具有多重继承"this"指针的说明
- 在noexcept 规范中是否允许使用"this"?
- 如何修复"error: ‘_1’ was not declared in this scope"?
- C++调用具有 *this 属性的单个帮助程序函数
- Doees the 'this' 指针参与虚函数的多态行为
- 在 c++ 中正确定义"this"关键字?
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 在什么情况下,两个堆栈分配的结构对象的 this 点指向同一个地址?
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- "Attribute is protected within this context"继承和 .h 和.cpp文件
- "is private within this context"被抛出用于不应是私有的函数(GCC 5.3.0,C++11)
- "error: within this context" 在Qt 5中安装QFtp
- 私有/受保护的变量"error: within this context"
- C++ gcc和Visual Studio的不同编译错误,'within this context'
- G++ 编译错误"... is protected from within this context"而 clang 没有错误