#include<iostream>
using namespace std;
class complex_number
{
    public: int x,*ptr;

  ostream & operator <<(ostream &out, complex_number &c)
    {
        out<<"size="<<c.x<<endl;
        for(int i=0;i<c.x;i++)
        out<<c.ptr[i]<<" ";
        out<<"\n";
        return out;
    }

    complex_number(int a)
    {
        x=a;
        ptr=(int *) malloc(a*sizeof(int));
        for(int i=0;i<5;i++)
        ptr[i]=i;
    }
};

int main()
{
    complex_number p(5);
    cout<<p;
}
above code show error "operator << must take only one argument"
and when i change code to

#include<iostream>
using namespace std;
class complex_number
{
    public: int x,*ptr;

   friend ostream & operator <<(ostream &out, complex_number &c);
    complex_number(int a)
    {
        x=a;
        ptr=(int *) malloc(a*sizeof(int));
        for(int i=0;i<5;i++)
        ptr[i]=i;
    }
};
ostream & operator <<(ostream &out, complex_number &c)
    {
        out<<"size="<<c.x<<endl;
        for(int i=0;i<c.x;i++)
        out<<c.ptr[i]<<" ";
        out<<"\n";
        return out;
    }

int main()
{
    complex_number p(5);
    cout<<p;
}
 then there is no error why so...?

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/_s8SKLlcB5UJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to