Here's my code
class B {
public:
void bar() {std::cout<<"~";}
};
class A {
public:
A() {b=B();};
A(B arg_b): b(arg_b) {};
void foo() {b.bar();};
private:
B b;
};
int main() {
A a;
a.foo(); // works fine
A aa(B());
aa.foo(); // could not compile, but if I comment out this line only, it can compile.
}
I got this error message
error: request for member ‘foo’ in ‘aa’, which is of non-class type ‘A(B (*)())’
aa.foo();
I'm a beginner in c++, could some one please explain why this code could not compile? and what is the correct way to initialize a class member by passing in an instance?