I'm writing Class A, and it has a constructor that initializes values. I'm creating an instance of Class A in another Class B like so:
#include <iostream>
class A {
public:
int value;
A(int value) {
this->value = value;
}
void print_string() {
std::cout << "talking...";
}
};
class B {
public:
A new_a;
B(int b_value) {
new_a = A(b_value);
}
void print_from_A() {
new_a.print_string();
}
};
int main() {
B b(23);
b.print_from_A();
return 0;
}
However, I have syntax
errors with this code, it says no default constructor exist for class A
. What I want is:
1 to make instance of class A global in class B so other methods in class B can access it.
2 to initialize values in class A through it's constructor by calling it in, and passing the values through class B