gpt4 book ai didi

c++ - 多重继承中没有匹配函数

转载 作者:行者123 更新时间:2023-11-30 02:28:36 25 4
gpt4 key购买 nike

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

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

类.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
}
};

类.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 以消除错误。

关于c++ - 多重继承中没有匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40602380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com