成员不能在当前作用域中定义

member cannot be defined in the current scope

本文关键字:作用域 定义 不能 成员      更新时间:2023-10-16

我目前正在为电子邮件地址的顶级域做检查。为了检查,我将它与一个文本文件列表进行比较。我想将列表导入到静态映射容器中。但是,当我尝试实例化它时,它说不能在当前作用域中定义它。为什么呢?

这是我的头文件:

    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整型静态成员或具有文本类型的构造表达式静态成员提供类内初始化式。