I have the following code:

ostream Person::operator<<(ostream& out, const Person& p)
{
           out << "/n*********************************" << endl;
           out << "Name: " << Person::getName() << endl;
           out << "Age: " << Person::getAge() << endl;
           out << "Health: " << Person::getHealthLevel() << "%" << endl;
           out << "Happiness: " << Person::getHappinessLevel() << "pts" << endl;
           out << "Sleep: " << Person::getSleepLevel() << "%" << endl;
           out << "Money: " << "$" << Person::getMoneyLevel() << endl;
           out << "Posessions: " << endl;
           out << "*********************************" << endl;

           return out;
}

It looks correct to me, but I'm getting a few errors:

`Person::operator <<(ostream &, const Person &)' must take exactly one argument
new declaration `class ostream Person::operator <<(ostream &, const Person &)'
ambiguates old declaration `class ostream & Person::operator <<(ostream &, const Person &)'
In method `ostream::ostream(const ostream &)':
`ios::ios(const ios &)' is private
within this context


Is there something I'm doing wrong?

Well. You should change the Person::function(). To p.function().

std::ostream &operator<<(std::ostream &stream,const Person &p)

Also, shouldn't you be using the p in your function? (Rather than calling static methods, you would want to use the information stored in the Person object p). (for example p.getName() )

commented: thanks for the help +3

I have the following code:

ostream Person::operator<<(ostream& out, const Person& p)
{
           out << "/n*********************************" << endl;
           out << "Name: " << Person::getName() << endl;
           out << "Age: " << Person::getAge() << endl;
           out << "Health: " << Person::getHealthLevel() << "%" << endl;
           out << "Happiness: " << Person::getHappinessLevel() << "pts" << endl;
           out << "Sleep: " << Person::getSleepLevel() << "%" << endl;
           out << "Money: " << "$" << Person::getMoneyLevel() << endl;
           out << "Posessions: " << endl;
           out << "*********************************" << endl;

           return out;
}

It looks correct to me, but I'm getting a few errors:

`Person::operator <<(ostream &, const Person &)' must take exactly one argument
new declaration `class ostream Person::operator <<(ostream &, const Person &)'
ambiguates old declaration `class ostream & Person::operator <<(ostream &, const Person &)'
In method `ostream::ostream(const ostream &)':
`ios::ios(const ios &)' is private
within this context
Is there something I'm doing wrong?

I think ostream class has a private copy constructor....that is why its giving you this error....you should return by reference....and ofcourse use p.function() as said by winbatch

commented: thanks for the help +3

Well you are probably getting that error because you are not making it a friend of your class it should look similar to this

#include <iostream>
#include <ostream>
#include <istream>
#include <string>

class Person
{
    public:
        Person();
        const std::string &getName() const;
        const int &getHealthLevel() const;
        const int &getHappinessLevel() const;
        const int &getSleepLevel() const;
        const unsigned int &getMoneyLevel() const;
        const unsigned int &getAge() const;
        friend std::ostream &operator<<(std::ostream &out,const Person &p);
    private:
        std::string name;
        int health;
        int happiness;
        int sleep;
        unsigned int money;
        unsigned int age;
};

Person::Person()
{
    name = "Brian";
    health = 10;
    happiness = -5;
    //Good lord I am tired
    sleep = -60000;
    //Shocker huh
    money = 0;
    age = 20;
}

const std::string &Person::getName() const
{
    return name;
}

const int &Person::getHealthLevel() const
{
    return health;
}

const int &Person::getSleepLevel() const
{
    return sleep;
}

const unsigned int &Person::getMoneyLevel() const
{
    return money;
}

const unsigned int &Person::getAge() const
{
    return age;
}

std::ostream &operator<<(std::ostream &out, const Person &p)
{
    out<<p.getName()<<std::endl;
    out<<p.getHealthLevel()<<std::endl;
    out<<p.getSleepLevel()<<std::endl;
    out<<p.getMoneyLevel()<<std::endl;
    out<<p.getAge()<<std::endl;
    
    return out;
}

int main()
{
    Person mainP;
    std::cout<<mainP;
    std::cin.get();
    return 0;
}
commented: thanks for the help +3

Thanks guys, that really helped a lot and cleared things up. It turned out that I didn't declare it as a friend. Now, one thing I don't understand is where the ostream& is passed since it's the first argument. I understand where Person& comes in, but I don't get where the ostream& is passed in. I know WHY it's there, but not sure where it comes from. Can anyone clear that up for me?

PS: I really appreciate the help from you guys and will be awarding rep points.

Thanks guys, that really helped a lot and cleared things up. It turned out that I didn't declare it as a friend. Now, one thing I don't understand is where the ostream& is passed since it's the first argument. I understand where Person& comes in, but I don't get where the ostream& is passed in. I know WHY it's there, but not sure where it comes from. Can anyone clear that up for me?

I hope you are asking for this....
when you do something like this

cout<<obj;//object of class

it is equivalent to

cout.operator<<(obj);

first argument passed is implicit this pointer and second one is obj

I hope you are asking for this....
when you do something like this

cout<<obj;//object of class

it is equivalent to

cout.operator<<(obj);

first argument passed is implicit this pointer and second one is obj

Ok, I think I got it. Correct me if I'm wrong:

The first parameter(ostream&) is actually on the right side of the operator << and the object it's operating on is on the right side of the << operator. Is that correct?

Ok, I think I got it. Correct me if I'm wrong:
The first parameter(ostream&) is actually on the right side of the operator << and the object it's operating on is on the right side of the << operator. Is that correct?

Its should be "left" and then "right"..you by mistake wrote "right" both times

Exactly what he said I mean you can overload the << to do something like this it is ugly as sin though I think.

#include <iostream>
#include <ostream>
#include <istream>

class Person
{
    public:
        Person()
        {
            age = 20;
        }
        std::ostream &operator<<(std::ostream &out)
        {
            out<<age<<std::endl;
            
            return out;
        }
    private:
        int age;
};

int main()
{
    Person a;
    a<<std::cout;
    
    std::cin.get();
    return 0;
}

Think of writting the overloaded << as an extension to the C++ ostream operators.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.