C++ の継承について私が理解していることは、子クラスのコンストラクターが呼び出されるたびに、親クラスのコンストラクターが自動的に呼び出されるということです。また、テンプレート化されたコンストラクタに関しては、テンプレート引数のデータ型は自動的に推測されます。つまり、テンプレート引数を個別に指定する必要はありません。プログラムは、私が理解していないように見えるコンパイル エラーを生成します。
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
class A{
public:
int x;
int y;
int first(){
return x;
}
int second(){
return y;
}
};
class C{
public:
float a,b;
C(){
a = 0.0f;
b = 0.0f;
}
template<class T>
C(T t){
a = t.first();
b = t.second();
}
};
class D: public C{
public:
float area(){
return a*b;
}
}
int main(){
A a;
a.x = 6;
a.y = 8;
C c(a);
D d(a);
cout<<c.a<<" "<<c.b<<" "<<d.area()<<endl;
}
コンパイルエラーが発生しました
test.cpp: In function ‘int main()’:
test.cpp:56:8: error: no matching function for call to ‘D::D(A&)’
test.cpp:56:8: note: candidates are:
test.cpp:44:7: note: D::D()
test.cpp:44:7: note: candidate expects 0 arguments, 1 provided
test.cpp:44:7: note: D::D(const D&)
test.cpp:44:7: note: no known conversion for argument 1 from ‘A’ to ‘const D&’
ここで何が起こっているのかわかりません。何か案は?