1
votes

I am learning operator overloading in c++ and I want to know the ouput of following code

#include<iostream>
using namespace std;
class xyz
{

 public:
        int i;

    friend ostream & operator<<( ostream & Out , int);
};



ostream & operator<<(ostream & out , int i)
{
    cout<<10+i<<endl;
}


int main()
{
    xyz A;
    A.i=10;

    cout<<10;
}

And i got two errors

  1. error: ambiguous overload for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘int’) cout<<10+i;

  2. error: ambiguous overload for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘int’) cout<<10;

can anyone explain whats the problem ?

I want to know that what happens if I overload an "<<" operator for printing int with only one parameter int(obvious) and I just want to print an number separately like "cout<<10" int the above mentioned code. So how compiler will decide that which function should be called when i am trying to print just any integer number.

2
there is already an overload for int plus the one you provided. What are you trying to achieve with the code? If you want to output an xyz then probably you want to write an overload for that type, not for its members - 463035818_is_not_a_number
I'm curious. Assuming you could replace the library provided overload with your own, how do you expect cout<<10+i<<endl; to not recurse without termination? - StoryTeller - Unslander Monica
there should also be a line number and file name (and with a navigation if use a good IDE). you can check there are two definition. - apple apple
tbh it is difficult to answer your question, because its not really clear why there should be no ambiguity ;). Maybe the question would be more clear if you explain why you think you would have to write a << overload for int - 463035818_is_not_a_number
You cannot just put random code and expect it to work, looks like you do not know what you are trying to do. Seems you try to follow an example but there operator would be overloaded for xyz not for int - Slava

2 Answers

2
votes

So obviously the problem is that you have written ostream & operator<<(ostream & out , int i) when this already exists. But it's clear that what you meant to write is this

ostream& operator<<(ostream& out, const xyz& a) // overload for xyz not int
{
    out<<a.i<<endl; // use out not cout
    return out;     // and don't forget to return out as well
}

and this

int main()
{
    xyz A;
    A.i=10;

    cout<<A<<endl; // output A not 10
}
0
votes

// this include brings std::ostream& operator<<(std::ostream&, int)
// into scope and therefore you cannot define your own later
#include<iostream>  

using namespace std;
class xyz
{

 public:
        int i;

    // needs body 
    friend ostream & operator<<( ostream & Out , int)
    {
        return Out;
    }
};



/* cant have this after including ostream
ostream & operator<<(ostream & out , int i)
{
    cout<<10+i<<endl;
}
*/


int main()
{
    xyz A;
    A.i=10;

    cout<<10;
}