我有一个类A接受B类作为构造函数参数.B类可以用int值构造.我原来的代码非常复杂,但我希望我把它简化为基本情况:
class B {
public:
explicit B(int a) : val(a) {}
private:
int val;
};
class A {
public:
A(const B & val) : value(val) {};
void print() {
//does nothing
}
private:
B value;
};
int main() {
int someTimeVar = 22;
A a(B(someTimeVar));
a.print();
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误代码:
$ g++ test.cpp -Wall -O0
test.cpp: In function ‘int main()’:
test.cpp:22:7: error: request for member ‘print’ in ‘a’, which is of non-class type ‘A(B)’
a.print();
^
test.cpp:20:9: warning: unused variable ‘someTimeVar’ [-Wunused-variable] …Run Code Online (Sandbox Code Playgroud)