Okay this makes more sense.
You are giving the + operator a
UInt32
as a rhs argument.
However, you do not have any version of the + operator that takes a
UInt32
. So the compiler has to convert that UInt32 to another type.
And it gets confused, because it could convert it to
int
or to
float
, and it doesn't know which one you want it to do. Hence, the call is ambiguous.
Your solution here is:
1) Add an overload to handle UInt32
or
2) Explicitly cast your UInt32 to something else:
1 2 3
|
SomeClass(UInt32 i) : SomeOtherClass(String("i = ") + static_cast<int>(i)) { }
// or
SomeClass(int i) : SomeOtherClass(String("i = ") + i) { }
|
Also note:
Your + operator is still broken because you are returning a reference to a temporary object. You should return by value:
1 2
|
String& operator+(const float rhs) const { String s = (*this); return s += rhs; } // <- bad, returning a temporary by reference
String operator+(const float rhs) const { String s = (*this); return s += rhs; } // <- good
|
I have also seen this syntax. Does it have any advantage over my version of operator+? |
Not really. It's just a different style -- although I think it might make a minor difference with how the compiler looks up the operator -- I don't know, I'm fuzzy on the details.. it's really obscure and hardly ever matters.
So it's basically the same thing.
Although that + operator is also broken because, again, it's behaving like += and modifying the lhs param.