成员不能在当前作用域中定义
member cannot be defined in the current scope
我目前正在为电子邮件地址的顶级域做检查。为了检查,我将它与一个文本文件列表进行比较。我想将列表导入到静态映射容器中。但是,当我尝试实例化它时,它说不能在当前作用域中定义它。为什么呢?
这是我的头文件:
class TldPart {
public:
static void LoadTlds();
private:
static map<string,bool> Tld;
}
下面是cpp中的实现:
void TldPart::LoadTlds()
{
map<string,bool> Tld;
...
}
它告诉我ValidTld不能在loadtld函数中定义
类的静态成员存在于对象之外。应该在类之外定义和初始化静态成员。
定义并初始化一个静态类成员:
头文件:
#pragma once
#include <map>
#include <string>
class TldPart {
public:
static void LoadTlds();
private:
static std::map<std::string, bool> tld;
};
cpp-file :
#include "external.h"
std::map<std::string,bool> TldPart::tld;
void TldPart::LoadTlds()
{
tld.insert(std::make_pair("XX", true));
}
别忘了课末加分号
编辑:您可以为const整型静态成员或具有文本类型的构造表达式静态成员提供类内初始化式。
相关文章:
- 块作用域中的函数指针定义
- 是否有 GCC 标志来发出有关作用域内相同变量重定义的警告?
- 为什么 for 循环不能定义两个作用域的变量?
- 作用域中的模板变量或模板类型定义
- 函数在作用域中定义,但编译器抱怨它超出了作用域
- 作用域中定义的变量与注入该作用域的命名空间变量
- 自定义条件作用域互斥锁
- 如何声明在函数作用域中定义的结构
- 在 FOR 循环之外定义的变量的作用域
- 我可以在函数作用域中使用定义指令吗?
- C++数组类定义和实现,未在作用域中声明
- C++11 Lambda闭包通过引用涉及一个堆栈变量,该变量离开作用域是允许的,但得到了未定义的行为
- 我的类未在作用域中定义?
- 如何在类的作用域中定义函数,而不是将其定义为成员函数
- 如何在与 c++ 中定义的函数作用域相同的文件中限制函数作用域
- 用户定义文字的作用域是什么
- 返回指向类定义的结构体的指针,多个作用域操作符
- 未在此作用域中声明的信号/对class::方法的未定义引用
- 类的作用域和定义顺序
- 在运行时定义非作用域文本