Operator Overload Confusion

I am new to the whole 'Operator Overload' world. I am attempting to overload the << operator but I'm unsure how to implement it properly. I have written a declaration and a function (not sure either work), but my real issue is how do I call this thing? Any help, advice, tips, and suggestions are greatly appreciated!

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class Statistician
{
    public:
        //Constructor
        Statistician();

        //Modifiers
        void Next_Number();
        void LengthOfSequence();
        void LastNumberOfSequence(double);
        void SumOfSequence(double);
        void SmallestOfSequence(double);
        void LargestOfSequence(double);
        void ResetSequence();
        void PrintAllSequence();
        double GetSum();
        double GetLength();
        double GetMean();
        double GetLast();

        // FRIEND FUNCTIONS
        friend ostream &operator<<(ostream &, Statistician &);

    private:
        int Length;
        double Number;
        double Last;
        double Sum;
        double Smallest;
        double Largest;
};

  //Declare variables
    Statistician S1, S2, S3;


    S1.Next_Number();

//!!!This is obviously wrong!!!
//What is the proper method for this?

    operator<< outs;


    //Exit program
    return EXIT_SUCCESS;
}

Statistician::Statistician()
{
        Length = 0;
        Number = 0.0;
        Last = 0.0;
        Sum = 0.0;
        Smallest = 0.0;
        Largest = 0.0;
}

void Statistician::Next_Number()
{
    //Declare variables
    char Another = 'Y';

    //Get User Info/Update
    while(Another == 'Y' or Another == 'y')
    {
        cout << "Please enter a number for the Statisitician: ";
        cin >> Number;
        cout << endl;

        LengthOfSequence();
        LastNumberOfSequence(Number);
        SumOfSequence(Number);
        SmallestOfSequence(Number);
        LargestOfSequence(Number);

        cout << "Would you like to enter another number (Y or N)? " ;
        cin >> Another;
    }//End while
}

void Statistician::LengthOfSequence()
{
    Length++;
}

void Statistician::LastNumberOfSequence(double N)
{
    Last = N;
}

void Statistician::SumOfSequence(double N)
{
    Sum = Sum + N;
}

void Statistician::SmallestOfSequence(double N)
{
    if(Length == 1)
    {
        Smallest = N;
    }
    else
    {
        if(N < Smallest)
        {
            Smallest = N;
        }
    }
}

void Statistician::LargestOfSequence(double N)
{
    if(N > Largest)
    {
        Largest = N;
    }
}

void Statistician::ResetSequence()
{
        Length = 0;
        Number = 0.0;
        Sum = 0.0;
        Smallest = 0.0;
        Largest = 0.0;
}

void Statistician::PrintAllSequence()
{
    cout << "The last number of the sequence is: " << Last << endl;
    cout << "The smallest number of the sequence is: " << Smallest << endl;
    cout << "The largest number of the sequence is: " << Largest << endl;
}

double Statistician::GetSum()
{
    return Sum;
}

double Statistician::GetLength()
{
    return Length;
}

double Statistician::GetMean()
{
    return Sum / Length;
}

double Statistician::GetLast()
{
    return Last;
}

ostream &operator<<(ostream &out, Statistician &s)
{
    out << "The last number of the sequence is: " << s.GetLast() << endl;

    return out;
}
It's done the same way it is with all the other << operators that ostream overloads. You just do this:

 
outs << S1;
I tried putting in:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    //Declare variables
    Statistician S1, S2, S3;


    S1.Next_Number();

    outs << S1;

    //Exit program
    return EXIT_SUCCESS;
}


But received error:

error:  outs is not declared in this scope


Do I need to declare outs as private in the class? Thanks for the help!
Last edited on
First of all it is better to declare the oprator the following way

ostream &operator<<(ostream &out, const Statistician &s)

In your test code use cout instead outs, because outs is not defined in your code.
Last edited on
That got rid of one error, but gave me a different one.

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
// FRIEND FUNCTIONS
// Modified this to fit code suggested by vlad from moscow (Thanks vlad)
friend ostream &operator<<(ostream &out, const Statistician &s);

int main()
{
    //Declare variables
    Statistician S1;

    S1.Next_Number();

    //Changed this according to vlad's suggestion
    cout << S1;

    //Exit program
    return EXIT_SUCCESS;
}


double Statistician::GetLast()
{
    return Last;
}

ostream &operator<<(ostream &outs, const Statistician &s)
{
    outs << "The last number of the sequence is: " << s.GetLast() << endl;

    return outs;
}



error:  passing 'const Statistician' as 'this' argument of 'double Statistician::GetLast()' discards qualifiers


Suggestions?
It's a const correctness issue.

if you have a const object (in this case, 's' in your << operator), you cannot call non-const member functions on it (since they might change its state, breaking the constness).

Therefore you can only call const members.

Since GetLast does not change anything in the class, you can make it const:

1
2
3
4
5
6
7
double GetLast() const; // prototype

// body
double Statistician::GetLast() const
{
...
}
Thanks! It works. I appreciate all the help given!
Topic archived. No new replies allowed.