gpt4 book ai didi

c++ - 错误 : type ‘class’ is not a direct base of ‘class’

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:05 25 4
gpt4 key购买 nike

我无法找到与我的问题具体相关的答案。就我所看到的而言,这是一个有点“奇怪”的案例。

所以我有一个继承自 Child1 的类 Child2,而 Child1 又继承自 ParentParent 没有构造函数。

这是我继承的方式:

Child1.h:

class Child1 : virtual public Parent

这是我想从 Child2 的构造函数中调用的函数:

void Child1::foo(unsigned int i)
{
// ...
}

child 2

Child2::Child2() : Child1()
{
foo(10);
}

Child2 中没有名为 foo() 的函数,所以如果我理解正确的话,应该不会有任何歧义。

但是,我遇到了这个错误:

Child2.cpp: In constructor ‘Child2::Child2()’: Child2.cpp:12:28: error: type ‘Child1’ is not a direct base of ‘Child2’ Child2::Child2() : Child1()

Child1 的构造函数正在运行,所以我将其排除在外。我不确定在这种情况下它是否重要。

最佳答案

检查您的 Child2 类是否被声明为 Child1 的子类。像这样的东西:

class Child2 : Child1 
{....}

关于c++ - 错误 : type ‘class’ is not a direct base of ‘class’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46616688/

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