我得到2套錯誤,試圖建立時:類繼承錯誤?
(第一版本)
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;
}
您需要使用初始化程序列表來初始化基礎。這應該在任何書或繼承教程中解決。 – chris
如果你沒有得到克里斯談論的內容:派生構造函數將調用基類默認構造器'bb:bb()'。既然你沒有指定'bb:bb()',它就不能構造'aa'的對象(它不知道如何構造它的基礎)。但是,您可以指定'aa :: aa(int)'應該使用'bb :: bb(int)':aa:aa(int _i):bb(_i){/ * ... * /}'。細節可以在任何像樣的書中找到。 – Zeta