多重继承中没有匹配函数

No matching function in multiple inheritance

我是 C++ 中继承的新手,决定尝试一些实验来了解这个主题。

下面的代码显示了我正在创建的 类 的层次结构:

classes.h

class base
{
protected:
    int _a;
    int _b;
    int _c;;
    base(int b, int c);
};

class sub_one : public virtual base
{
public:
    sub_one(int a, int b) : base(a, b)
    {
        // do some other things here
    }

    // other members
};


class sub_two : public virtual base
{
protected:
    int _d;
public:
    sub_two(int a, int b, int c = 0) : base(a, b) 
    {
        // do something
    }

    // other members
};


class sub_three : public sub_one, public sub_two
{
private:
    bool flag;
public:
    sub_three(int a, int b, int c = 0) : base(a, b) 
    {
        // do something    
    }
};

classes.c

base::base(int a, int b)
{
    // ...
}

编译器显示消息:

no matching function for call to sub_one::sub_one()

no matching function for call to sub_one::sub_one()

no matching function for call to sub_two::sub_two()

no matching function for call to sub_two::sub_two()

我就是找不到问题所在。

sub_three(int a, int b, int c = 0) : base(a, b) 
{
    // do something    
}

相当于:

sub_three(int a, int b, int c = 0) : base(a, b), sub_one(), sub_two() 
{
    // do something    
}

由于sub_onesub_two中没有这样的构造函数,编译器报错。您可以将默认构造函数添加到 sub_onesub_two 以消除错误。

sub_three构造函数初始化base,并调用不存在的sub_onesub_two的默认构造函数,可能需要

class sub_three : public sub_one, public sub_two
{
private:
    bool flag;
public:
    sub_three(int a, int b, int c = 0)
       : base(a, b), sub_one(a,b), sub_two(a,b,c), flag(false)
    {
        // do something    
    }
};