Operator overloading problem

Upon attempting an operator overloadulation (yes, I know, not a word), I get an error stating that
ostream & operator<<(ostream &, nation) must take exactly one argument

I know what this means (I think), but I have no idea how to fix it. Also, my other error arose due to this, that there is no match for operator<<() in std::cout << yours. Any ideas? Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
using namespace std;

class nation
{
    string name;
    long population;
    long size;
    bool borders_water;
    bool island;
    public:
nation(string n, int p = 1000, int s = 1000, bool b = true, bool i = false)
    {
        name = n;
        population = p;
        size = s;
        borders_water = b;
        island = i;
    }
void setpop(int p)
    {
        population = p;
    }
void setsize(int s)
    {
        size = s;
    }
void addpop(int p)
    {
        population += p;
    }
void addsize(int s)
    {
        size += s;
    }
ostream & operator<<(ostream & os, nation obj)
{
        os << obj.name << endl << "Population: " << obj.population << endl << "Area: " << obj.size << endl << "It does";
       if(!obj.borders_water)
    {
        os << "n't border water\n";
    }
        else
    {
        os << " border water\nIt is";
        if(!obj.island)
        {
            os << "n't";
        }
        os << " an island";
    }
    return os;
}
};

int main()
{
    cout << "Name of your nation: ";
    string name;
    getline(cin, name);
    cout << "Population: ";
    long pop;
    cin >> pop;
    cout << "Area: ";
    long ar;
    cin >> ar;
    cout << "Does it border water? [0 for false, 1 for true]: ";
    bool b;
    cin >> b;
    bool i;
    if(b == 1){
    cout << "Is it an island? [0 for false, 1 for true]: ";
    cin >> i;}
    else
    i == false;
    nation yours(name, pop, ar, b, i);
    cout << yours;
    cin.get();
    cin.get();
    return 0;
}
I must admit I'm not exactly an expert at dealing with streams like this, but it's just that. operator<< only takes one parameter, which is the right hand operand (the type of the left hand is determined by the class that defined the operator).
Add 'friend' in front:
friend ostream & operator<<(ostream & os, nation obj)
And pass the nation object by const reference, please.

 
friend ostream& operator<<( ostream& os, nation const& obj ) { /*...*/ }

I'm no pro with overloading operators but it might just be better to do it externally instead of making it a friend of the class nation.

Also another way to format output is to use a member function that takes a ostream& object

1
2
void display(ostream& out) {
out << name << endl << "Population: " << population
etc.

Then when you want to display it overload the operator<< do externally:

1
2
3
4
ostream& operator<<(ostream& o, nation& n) {
n.display(o);
return o;
}


Then just use the overloaded in the main function with the object
cout << nation;
Last edited on
Mahlerfive's solution worked, sorry for not saying so earlier.

Thanks.
Topic archived. No new replies allowed.