Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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++ 抽象类成员可以';派生类中的t访问值集_C++_G++ - Fatal编程技术网

C++ 抽象类成员可以';派生类中的t访问值集

C++ 抽象类成员可以';派生类中的t访问值集,c++,g++,C++,G++,我有以下抽象类 class Language { const std::string name ; protected: std::string cmd, home ; Config::Section cfg ; bool load_conf() { home = env("HOME") ; // DEBUG std::cout << home << std::endl ;

我有以下抽象类

class Language {
    const std::string name ;

    protected:
    std::string cmd, home ;
    Config::Section cfg ;

    bool load_conf() {
        home = env("HOME") ;

        // DEBUG
        std::cout << home << std::endl ;
        std::cout << name << std::endl ;

        if (!cfg.load(home + "/.cr", name)) {
            std::cerr << "cr: No configuration found for this language." << std::endl ;
            return false ;
        }

        return true ;
    }

    public:        
    virtual bool handles(const std::string) = 0 ;
    virtual int run(std::string) = 0 ;
} ;
我遇到的问题是在
load_conf()
中,由于某种原因,
name
报告为空,这使得即使配置良好,它也会抛出一个错误
load_conf()
从每个派生类实现的
run()
调用

我试着把
命名为受保护的和公开的,但两者似乎都没有任何区别。理想情况下,我只想在抽象类(受保护)中声明
name
,而不必在每个派生类中重复声明,但当我尝试时,它不会编译。我也尝试过使用
this->name
,但那也是空的,删除
const
也没什么区别


我觉得这是一个范围问题,但我缺少什么呢?

在基类中声明一个构造函数,它接受一个参数,即初始化
名称的值,并将其传递到派生的构造函数中

以下是一个例子:

class Language {
    const std::string name ;
};

class Python : public Language {
    const std::string name ; // <-- this is different, and frankly redundant
};
类语言{
常量std::字符串名称;
};
类Python:公共语言{

const std::string name;//在基类中声明一个构造函数,该构造函数接受一个参数,即初始化
name
并将其传递给派生的构造函数的值

以下是一个例子:

class Language {
    const std::string name ;
};

class Python : public Language {
    const std::string name ; // <-- this is different, and frankly redundant
};
类语言{
常量std::字符串名称;
};
类Python:公共语言{

const std::string name;//当您在
load_conf()
中引用
name
时,在基类
Language
中已经有一个名为
name
的变量,而不是派生类
name
变量

简单的解决方案是为两个变量分别命名或

将基类中的
名称
设为
受保护
,以便每个派生类都可以引用此变量。

当您在
加载配置()中引用
名称
时,基类中已经有一个名为
名称
的变量
编译器引用基类
name
变量,而不是派生类
name
变量

简单的解决方案是为两个变量分别命名或

将基类中的
名称
设为
受保护
,以便每个派生类都可以引用此变量。

类Python中
覆盖的是
名称
字段,该字段不是
加载配置
引用的字段


使用
语言将
name
设为受保护的
class
protected
并删除其子类中的一个,它应该可以正常工作。

类Python
中,您正在覆盖
name
字段,该字段不是
load\u conf
引用的字段

使用
语言
protected
创建
名称
,并删除其子类中的一个,它应该可以正常工作

class Language {
    const std::string name ;
    Language(std::string some_name) : name(some_name) {}  // <-- set the name
};

class Python : public Language {
    Python() : Language("python") {} // construct the base with the name...
};