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
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.
No comments:
Post a Comment