Saturday, 14 December 2019

c++ - Ambiguous Overload For operator "

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


#include
using namespace std;
class xyz
{
public:
int i;
friend ostream & operator<<( ostream & Out , int);
};
ostream & operator<<(ostream & out , int i)
{
cout<<10+i<}
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.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...