c++ - 类继承出错?

标签 c++ qt

我在尝试构建时遇到了两组错误:

(在第一次构建时)

In constructor 'aa::aa(int)':
no matching function for call to 'bb:bb()'
candidates are: bb::bb(int)
bb:bb(const bb&)

(然后我再次点击构建并得到以下内容)

file not recognized: File truncated... takes me to assembly
collect2:ld returned 1 exit status

#ifndef BB_H
#define BB_H

class bb
{
public:
    bb(int _m);
    int m;
};

#endif // BB_H

#ifndef AA_H
#define AA_H
#include "AA/bb.h"

class aa :  bb
{

public:
    aa(int _i);

    int i;
    int j;
};

#endif // AA_H

#include "bb.h"

bb::bb(int _m)
{
    m = _m * 5;
}

#include "aa.h"

aa::aa(int _i)
{
    i = _i;
    j = i + 1;
}

最佳答案

In constructor 'aa::aa(int)':
no matching function for call to 'bb:bb()'

编译器的权利。您没有默认构造函数。即使您不编写默认构造函数,编译器也会为您编写默认构造函数,但如果您有任何用户定义的构造函数,则不会发生这种情况。

你有两个选择:

首先,实现一个默认构造函数:

class bb
{
public:
    bb(int _m);
    bb();
    int m;
};

bb:bb()
{
}

这可能很恶心,因为您将如何初始化 m

其次,使用初始化列表在aa 的构造函数中调用转换构造函数:

aa::aa(int _i)
:
  bb (_i)
{
    i = _i;
    j = i + 1;
}

关于c++ - 类继承出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17620473/

相关文章:

c++ - 'auto ... arg' 的参数包形式在 lambda 中启用但在函数中没有启用?

c++ - 在 C++ 中创建具有按引用调用返回值的类函数

c++ - 如何返回 Qt 的 main ?

c++ - 为什么 QFile 不能从 "~"目录读取?

c++ - 基于uniform int在GLSL中声明数组

c++ - Boost ASIO 异步接受器不打开监听端口

c++ - Int& 到 const int -static 与 dynamic-

c++ - QString::startWith() 和 QStringList::removeAll() 未按预期运行

c++ - Qt5 加载 .qm 翻译文件