Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法在当前作用域中定义成员_C++ - Fatal编程技术网

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是的,但我不能确定作者包含了它,考虑到他漏掉了一个分号。