When I write a constructor of a class which has an other class' object, like in the example below, I get the compiling error error: no matching function for call to 'A::A()'
class A {
int x;
public:
A(int _x) {
this->x=_x; } };
class B {
A obj;
public:
B(int x) {
obj=A(x); } };
int main(){}
I know that by adding a constructor of A with no parameters (like A(){}
) i would solve the problem, but there is another way to solve it without introducing a new constructor?
p.s.: i know using a pointer to A insted of a object of class A, would solve, but I want to know if there's a way keeping the object.