Skip to main content
GameDev.net gamedev.net
🔒 Locked

Ambiguous operator call.

Started by FlaiseSaffron Aug 5, 2006 at 11:27 AM 2 replies 700+ views
Original Post
FlaiseSaffron
FlaiseSaffron
I'm getting a bizarre compiler error and I don't understand why it's happening.

template<class I> class Array {
	public:
		__inline operator I*() const {
			return ptr;
		}

		...
};

class Entity {
	public:
		__inline const Array<char>& getName() const {
			return name;
		}

		...
};

inline ostream& operator <<(ostream& out,const Entity& printMe) {
	return out << printMe.getName();
}

...


The compiler error: error C2593: 'operator <<' is ambiguous On the other hand, when I explicity cast the Array to a char* like so:

	return out << static_cast<char*>(printMe.getName());


..it compiles fine, and when I get rid of the operator and write it out by hand, for example:

	cout << Array<char>("Hello, world.\n");


..it works fine; it's only with the 'operator <<(ostream&,const Entity&)' that I get ambiguity. Why is this happening?
JohnBolton
JohnBolton
Because of your conversion operator (Array to I *), the compiler can convert an Array to char * any time it might need to. When the compiler sees "cout << foo", where foo is Array, it doesn't know which version of operator<< to use. It can use either operator<<(ostream&,const Entity&) or operator<<(ostream&,const char *)".
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
deathkrush
deathkrush
It works fine in my compiler, Visual Studio .NET 2005. Maybe you could try something like this instead of defining a cast operator:

template<class I>inline ostream& operator <<(ostream& out,const Array& printMe) {	return out << printMe.ptr;}
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
Enigma
Enigma
This is why user-defined implicit conversions are usually a bad idea. They create unexpected ambiguities.

Σnigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.