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
error: ambiguous overload for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘int’) cout<<10+i;
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.
intplus the one you provided. What are you trying to achieve with the code? If you want to output anxyzthen probably you want to write an overload for that type, not for its members - 463035818_is_not_a_numbercout<<10+i<<endl;to not recurse without termination? - StoryTeller - Unslander Monica<<overload forint- 463035818_is_not_a_numberxyznot forint- Slava