hi, I saw this question in homework:
A.h:
#include <iostream>
using namespace std;
class A
{
public:
A(int a_value)
: a{a_value}
{ cout<<"A constructor\n"; }
~A()
{ cout<<"A destructor\n"; }
int a;
};
B.h:
#include <iostream>
using namespace std;
class B
{
public:
B(const A& A_obj_value, int b_value)
: A_obj{A_obj_value}, b{b_value}
{ cout<<"B constructor\n"; }
~B()
{ cout<<"B destructor\n"; }
A A_obj;
int b;
};
main.cpp:
#include "A.h"
#include "B.h"
int main()
{
A{1};
B{A, 2};
return 0;
}
Original question: "What is the output of the above program?"
The answer key says the output is supposed to be the following:
A constructor
B constructor
B destructor
A destructor
A destructor
so I don't just need the answer, my problem is that I don't understand why that is the answer. So I want to compile the program and add more print statements or step through it in debugger to try to understand what the program is doing, but unfortunately the program does not compile for me and I don't know how to make it compile. When I use g++ main.cpp I always see errors like this:
main.cpp: In function ‘int main()’:
main.cpp:7:11: error: no matching function for call to ‘B::B(<brace-enclosed initializer list>)’
7 | B{A, 2};
| ^
I tried changing some of the code to add object declarations or change curly braces to parentheses, but that did not help. Do you know what is needed for me to add in order to test this code?