C++ 无法在当前作用域中定义成员
我目前正在做一个顶级域名的电子邮件地址检查。为了进行检查,我将其与文本文件列表进行比较。我想将列表导入到静态映射容器中。但是,当我尝试实例化它时,它说它不能在当前范围中定义。为什么呢 这是我的一些头文件:C++ 无法在当前作用域中定义成员,c++,C++,我目前正在做一个顶级域名的电子邮件地址检查。为了进行检查,我将其与文本文件列表进行比较。我想将列表导入到静态映射容器中。但是,当我尝试实例化它时,它说它不能在当前范围中定义。为什么呢 这是我的一些头文件: class TldPart { public: static void LoadTlds(); private: static map<string,bool> Tld; } 类TldPart{ 公众: 静态无效载荷
class TldPart {
public:
static void LoadTlds();
private:
static map<string,bool> Tld;
}
类TldPart{
公众:
静态无效载荷tlds();
私人:
静态地图Tld;
}
以下是cpp中的实施:
void TldPart::LoadTlds()
{
map<string,bool> Tld;
...
}
void TldPart::LoadTlds()
{
地图Tld;
...
}
它告诉我不能在LoadTlds函数中定义ValidTld。类的静态成员存在于对象之外。您应该在类之外定义和初始化静态成员 在这里,我们定义并初始化静态类成员: 头文件:
#pragma once
#include <map>
#include <string>
class TldPart {
public:
static void LoadTlds();
private:
static std::map<std::string, bool> tld;
};
#include "external.h"
std::map<std::string,bool> TldPart::tld;
void TldPart::LoadTlds()
{
tld.insert(std::make_pair("XX", true));
}
#pragma一次
#包括
#包括
类TldPart{
公众:
静态无效载荷tlds();
私人:
静态std::map tld;
};
您的cpp文件:
#pragma once
#include <map>
#include <string>
class TldPart {
public:
static void LoadTlds();
private:
static std::map<std::string, bool> tld;
};
#include "external.h"
std::map<std::string,bool> TldPart::tld;
void TldPart::LoadTlds()
{
tld.insert(std::make_pair("XX", true));
}
#包括“external.h”
std::map TldPart::tld;
void TldPart::LoadTlds()
{
tld.insert(标准::制作配对(“XX”,真));
}
别忘了在课后加上分号
编辑:您可以为const integral类型的静态成员或constexpr类型且具有文字类型的静态成员提供类内初始值设定项。类的静态成员存在于对象外部。您应该在类之外定义和初始化静态成员 在这里,我们定义并初始化静态类成员: 头文件:
#pragma once
#include <map>
#include <string>
class TldPart {
public:
static void LoadTlds();
private:
static std::map<std::string, bool> tld;
};
#include "external.h"
std::map<std::string,bool> TldPart::tld;
void TldPart::LoadTlds()
{
tld.insert(std::make_pair("XX", true));
}
#pragma一次
#包括
#包括
类TldPart{
公众:
静态无效载荷tlds();
私人:
静态std::map tld;
};
您的cpp文件:
#pragma once
#include <map>
#include <string>
class TldPart {
public:
static void LoadTlds();
private:
static std::map<std::string, bool> tld;
};
#include "external.h"
std::map<std::string,bool> TldPart::tld;
void TldPart::LoadTlds()
{
tld.insert(std::make_pair("XX", true));
}
#包括“external.h”
std::map TldPart::tld;
void TldPart::LoadTlds()
{
tld.insert(标准::制作配对(“XX”,真));
}
别忘了在课后加上分号
编辑:您可以为const integral类型的静态成员或constexpr类型且具有文字类型的静态成员提供类内初始值设定项。您可以发布准确的错误消息吗?您可以发布准确的错误消息吗?很好,但是
#pragma once
和包括“stdafx.h”
是非标准的,还有,不要在这里做任何事情。@Potatoswatter是的,但我不能确定作者是否包含了它,因为他漏掉了一个分号。很好,但是#pragma once
和#include“stdafx.h”
是非标准的,并且无论如何不要在这里做任何事情。@Potatoswatter是的,但我不能确定作者包含了它,考虑到他漏掉了一个分号。