When I try to compile the following code:
class a {
int i;
public :
a(int);
};
class b {
a mya;
int j;
public:
b(int);
};
a::a(int i2) {
i=i2;
}
b::b(int i2) {
mya=a(i2);
j=2*i2;
}
int main() {
}
I get the following errors:
prog.cpp:21:12: error: no matching function for call to ‘a::a()
b::b(int i2) {
^
prog.cpp:17:1: note: candidate: ‘a::a(int)
a::a(int i2) {
^
prog.cpp:17:1: note: candidate expects 1 argument, 0 provided
prog.cpp:1:7: note: candidate: ‘constexpr a::a(const a&)’
class a {
^
prog.cpp:1:7: note: candidate expects 1 argument, 0 provided
prog.cpp:1:7: note: candidate: ‘constexpr a::a(a&&)
prog.cpp:1:7: note: candidate expects 1 argument, 0 provided
It seems that a constructor with no argument for the class a is expected. I do not understand why, the only time I create a object of type a, I call the constructor which takes an int as argument.
I understand that the solution would be to add a constructor without arguments for a. But why ?
Thank you for your answers, best regards,
Jerome
Aucun commentaire:
Enregistrer un commentaire