C++ compilation error: cannot convert from B to A, no constructor, or constructor overload ambiguity
From what I understand, the compiler tries several paths to interpret a = (A)b
.
- it finds the
operator A
- but it also finds the
operator int
on B, and theA(int)
constructor which gives it a second pathB => int => A
...
And it does not know which to pick.
To fix the compilation, I can:
- remove the
operator int
from B - rewrite the error line as
A a = b.operator A();
...
The error message means that these two operators
operator A(void) const { return A(_m); }
operator int(void) const { return _m; }
can be used in the expression
(A)b;
As a result using these conversion operators there can be used either the constructor A( int )
or the default copy constructor A( const A & )
.
To make it more clear rewrite the corresponding declaration like
A a = A( b );
So whether the object b is converted to an object of the type A using the first conversion operator or to an object of the type int using the second conversion operator.
You could avoid the ambiguity declaring the operators for example like
operator A(void) const & { return A(_m); }
operator int(void) const && { return _m; }
that is for lvalues the first operator will be used and for rvalues the second operator will be used.
Here is your program with the modified operators.
#include <iostream>
class A
{
public:
A(void) :_m(0) { }
A(int val) : _m(val) {}
private:
int _m;
};
class B
{
public:
B(void) : _m(0) {}
B(int val) : _m(val) {}
B(const A&);
// there is a direct conversion operator here
operator A(void) const & { return A(_m); }
operator int(void) const && { return _m; }
private:
int _m;
};
int main()
{
B b;
A a = b;
A a1 = B();
}