Update: The proposed duplicate resolves only part of this question. The key to understanding what is happening (the fact that a temporary link is first created) is not explained there.
This is my first time with implicit conversions, so I wrote this:
class A {};
class B {
public:
B(A& other) {}
B(const B& other) {}
};
int main() {
A foo;
B bar = foo;
}
This compiles as expected, but if I remove it const
, my compiler (gcc version 4.8.4) throws at destination, with an error message that I cannot understand:
test.cc: In function ‘int main()’:
test.cc:12:13: error: no matching function for call to ‘B::B(B)’
B bar = foo;
^
test.cc:12:13: note: candidates are:
test.cc:7:5: note: B::B(B&)
B(B& other) {}
^
test.cc:7:5: note: no known conversion for argument 1 from ‘B’ to ‘B&’
test.cc:5:5: note: B::B(A&)
B(A& other) {}
^
test.cc:5:5: note: no known conversion for argument 1 from ‘B’ to ‘A&’
Is this C ++ code? Why does he say no matching function for call to ‘B::B(B)’
when I try to appoint A
for a start?
source
share