overload << operator

I get an error trying to overload <<

class Test
{
private:
      int value;

public:
      Test();
      Test(int value);

      ostream& operator <<(ostream &os,const Test &obj);
};

ostream& Test::operator << (ostream &os,const Test &obj)
{
    os << obj.value;
    return os;
}

my error:
error C2804: binary 'operator <<' has too many parameters
LVL 5
lwinkenbAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ikeworkCommented:
hi lwinkenb,

you have to overload the global operator<< and make it a friend in your class, not implement it as a member of your class:


class Test
{
    friend ostream& operator<< (ostream &os,const Test &obj);

private:
     int value;

public:
     Test();
     Test(int value);

     ostream& operator <<(ostream &os,const Test &obj);
};


ostream& operator<< (ostream &os,const Test &obj)
{
    os << obj.value;
    return os;
}


hope it helps :)
ike

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ikeworkCommented:
oops forgot to remove your old member-operator in the class-definition, must be:

class Test
{
    friend ostream& operator<< (ostream &os,const Test &obj);

private:
     int value;

public:
     Test();
     Test(int value);
};


ostream& operator<< (ostream &os,const Test &obj)
{
    os << obj.value;
    return os;
}


ike
lwinkenbAuthor Commented:
do all overloaded operators need to be declared as "friend", or just the stream ones?
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

ikeworkCommented:
only if they access a private/protected variable of the class, if you put a publuc accessor-method to the class, then you dont need it.

this way you dont need it:

class Test
{
private:
     int value;

public:
     Test();
     Test(int value);

     // now the operator uses this ...
     int GetValue() { return value; }
};

ostream& operator<< (ostream &os,const Test &obj)
{
    os << obj.GetValue();
    return os;
}


ike
lwinkenbAuthor Commented:
I understand now, thanks.  
ikeworkCommented:
you're welcome :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.