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

Overloading operator is ambiguous?

Started by silverphyre673 Mar 13, 2005 at 11:06 PM 3 replies 3.3k views
Original Post
silverphyre673
silverphyre673
I have a console class with pointers to cin and cout. I have this function:

template<class T>
Console & operator << ( Console & console, T & writeObj)
{
    console.outstream << writeObj;
    return console;
}

int main()
{
    string str = " hello ";
    Console console;
    console << "\n" << str << "\n";
}

Which gives me the errors: ambiguous overload for 'operator<<' in 'console << "\n"' std::ostream& operator<<(Console&, T&) [with T = const char[2]] How should I fix this? Thanks
my siteGenius is 1% inspiration and 99% perspiration
Erzengeldeslichtes
Erzengeldeslichtes
I believe the problem is that it doesn't like referances to static arrays.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Fruny
Fruny
Why is the return type in the error message an std::ostream&? I'm sure there is code you haven't shown us...

Like, does Console derive from std::ostream?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
silverphyre673
silverphyre673
Well, I've changed a few things around...

Now, the write function (which works) looks like this:

template<class T>void Consol::Write( T & writeObj){    cout << T;}


What should I do to make it so that

a) You could pass an std::string to Write() and have it work

b) I could get this to work in the same manner as cout works:

Console console;string str1 = "my name is Ben."console << "Hello, World! " << str1 << "\n";


Thanks! Tell me if you need more info
my siteGenius is 1% inspiration and 99% perspiration
Erzengeldeslichtes
Erzengeldeslichtes
std::ostream& operator<<(std::ostream& ls, const std::string& rs){    return ls << rs.c_str();}

Is how you make it work with std::string.
Or you can generalize that like I do:
template<typename _Elem, typename _Traits, typename _Ax>std::basic_ostream<_Elem, _Traits>& operator<<(  \         std::basic_ostream<_Elem, _Traits>& ls,  \         const std::basic_string<_Elem, _Traits, _Ax>& rs){	return ls << rs.c_str();}

Which of course makes it work with std::ofstream, std::wcout, std::wofstream, std::wstring, etc., so long as the elements and traits match (it gets wonky if they don't, which is why the template forbids them from being different)

Try:
template<class T>Console& operator<<(Console& ls, T& rs){    ls.Write(rs);    return ls;}

If the Write code you posted worked (which it shouldn't since console was mispelled and you're outputting the type rather than the variable), then this should work.

Edit:
I notice in your OP you said "pointers to cin and cout", so "console.outstream" is a std::ostream*? If so, then a problem (if not the problem) was that you're doing a shift operation on the pointer. You should dereferanced the pointer, so it would look something like this:
class Console{    //...    std::ostream* outstream;    //...    Console()    {    //...        outstream = &std::cout;    //...    }    //...};//...template<class T>Console& operator<<(Console& ls, T& rs){    (*ls.outstream) << rs;    return ls;}


[Edited by - Erzengeldeslichtes on March 14, 2005 5:16:30 AM]
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?

Topic Locked

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

Sign in to reply to this topic.