简体   繁体   English

成员初始化程序“ SuperClass”未命名非静态数据成员或基类

[英]member initializer 'SuperClass' does not name a non-static data member or base class

I have a problem with some of my constructors. 我的一些构造函数有问题。 Both subclasses need to get the same classes (no super class), that is why these classes should be initialized in the super class: 这两个子类都需要获得相同的类(没有超类),这就是为什么这些类应在超类中初始化的原因:

template<typename T, typename S>   
class SuperClass {
protected:
    OtherClass <T> const& _class1;
    OtherOtherClass <T> const& _class2;

    SuperClass() {

    }

    SuperClass(OtherClass<T> const& class1, OtherOtherClass<T> const& class2)
        : _class1(class1), _class2(class2)
    {
            // Alternative I tried:
            // this->_class1 = class1;
            // this->_class2 = class2;
    }

I tried to use it through: 我尝试通过以下方式使用它:

    template<typename T, typename S> 
    class SubClass1 : public SuperClass<T, S> {
    private:
        someFunc() {
            return this->_class1.getSomething(); // as an example
        }

    public:
        SubClass1(OtherClass<T> const& class1,
                OtherOtherClass<T> const& class2)
                : SuperClass(class1, class2)
            {
                // some definitions
            }

     }

After that this error shows up: 之后,出现此错误:

member initializer 'SuperClass' does not name a non-static data member or base class 成员初始化程序“ SuperClass”未命名非静态数据成员或基类

I found some people with similar problems, but it did not lead me to the solution. 我发现有些人有类似的问题,但这并没有带我解决问题。 For example: member initializer does not name a non-static data member or base class I did not see many difference there and tried to add an empty constructor like he did. 例如: 成员初始值设定项未命名非静态数据成员或基类,我在那没有看到很多区别,因此尝试像他一样添加空的构造函数。

The error says it all: 错误说明了一切:

member initializer ' SuperClass ' does not name a non-static data member or base class 成员初始值设定项“ SuperClass ”未命名非静态数据成员或基类

SuperClass is not a class. SuperClass不是类。 It's a class template . 这是一个类模板 As such, it's not the base class of your type. 因此,它不是您类型的基类。 The base class is a specific instantiation of the class template: SuperClass<T,S> . 基类是类模板的特定实例: SuperClass<T,S> That's what you need: 那就是你所需要的:

    SubClass1(OtherClass<T> const& class1,
            OtherOtherClass<T> const& class2)
    : SuperClass<T,S>(class1, class2)
    //          ^^^^^

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当初始值设定项是基类名称时出现错误“初始值设定项未命名非静态数据成员或基类” - Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name 成员初始值设定项不命名非静态数据成员 - Member initializer does not name a non-static data member 模板类和继承问题-“列表”未命名非静态数据成员或基类 - Issues with template classes and inheritance - 'List' does not name a non-static data member or base class 枚举不是类的非静态数据成员或基类 - enum is not a non-static data member or base class of class 成员初始化程序列表和非静态数据成员上的默认成员初始值设定项之间的区别是什么? - What's the differences between member initializer list and default member initializer on non-static data member? 非静态数据成员初始值设定项中lambda函数的分段错误 - Segmentation fault for lambda function in non-static data member initializer 部分聚合初始化和非静态数据成员初始化程序 - Partial Aggregate Initialization and Non-static Data Member Initializer 来自另一个非静态的非静态成员初始化程序 - Non-static member initializer from another non-static 是否允许在默认成员初始化程序中调用非静态成员 function? - Is it allowed to call a non-static member function in a default member initializer? 该类中已删除的析构函数显示为虚拟/直接基类或非静态数据成员的类型 - Deleted destructor in the class appeared as a virtual/direct base class or as a type of non-static data member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM