Overloaded + operator error

Hello all it has been a long time since I have asked a question on here, but I am having trouble with this overloaded + operator. I am trying to combine the two objects and return a new object. when my code acesses this function it fails. What did I do wrong? Sorry about my bad spelling

1
2
3
4
5
6
7
  
 statistician operator + (const statistician & s1, const statistician & s2)
   {
       statistician s3;
       s3.next(s1.sum() + s2.sum());
       return s3;
   }
What does sum() return?
sum returns the total numbers stored in the object. not the addition of all the numbers just how many there are.
it fails

What on earth does that mean?

I'm curious - what led you to the conclusion that not giving us detailed, specific information about how it's failing, was the best way to get our help?
Last edited on
What type does sum() return?
sorry about that its been about a year since I have posted anything on here. It fails meaning I do not get a new object with the length of s1 + s2. I truly thought the problem was in the function that I posted.
sum returns a double

here is the rest of my code, sorry for being obscure i really thought it was in that function.

.h file
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
#ifndef STATS_H     // Prevent duplicate definition
#define STATS_H
#include <iostream>

namespace jesseb303 
{
    class statistician
    {
    public:
        // CONSTRUCTOR
        statistician( );
        // MODIFICATION MEMBER FUNCTIONS
        void next(double r);
        void reset( );
        // CONSTANT MEMBER FUNCTIONS
        int length( ) const;
        double sum( ) const;
        double mean( ) const;
        double minimum( ) const;
        double maximum( ) const;
        // FRIEND FUNCTIONS
        friend statistician operator + (const statistician& s1, const statistician& s2);
        friend statistician operator * (const statistician& s1, const statistician& s2);
        friend bool operator ==(const statistician& s1, const statistician& s2);
   
    private:
        int count;       // How many numbers in the sequence
        double total;    // The sum of all the numbers in the sequence
        double tinyest;  // The smallest number in the sequence
        double largest;  // The largest number in the sequence
    };

   
   
}

#endif 



implementation file
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
//FILE: stats.cpp
// CLASS IMPLEMENTED: stats
 
#include "stats.h" // provides the stats class definition
 
namespace jesseb303
{
   statistician::statistician() : count(0), total(0), tinyest(0), largest(0)
   {}                                          //initializing everything to 0
 
   /*-----------------------------------------------------------------------+
   | next(double r)
   |
   | Give the statistician a new number
   |
   | Post: the count of the numbers statistician has seen has been
   |    incremented
   |       the sum of all the numbers statistician has seen has been
   |    increment by r
   |       the values of tinyest and largest have been adjusted if
   |    necessary
   */
   void statistician::next(double r)
   {
       if (count <= 0) {                       // update the number of entries statistician has
           count = 1;                          // this is the first data item of the statistician
           total = r;
           tinyest = r;
           largest = r;
           return;
       }
       count += 1;
       total += r;                             // adjust the sum
       if (r < tinyest) {                      // update the min if necessary
           tinyest = r;
       }
       if (largest < r) {                      // update the max if necessary
           largest = r;
       }
   }
 
   void statistician::reset( )
   {
       count = 0;                              // resets all values back to 0
       tinyest = 0;
       largest = 0;
       total = 0;
   }
 
       /*-------------------------------------------------------------------+
       | int length( ) const
       |
       | RETURN count which is the length of the sequence that has
       |    been given to the statistician (i.e., the number of times that the
       |    next(r) function has been activated).
       */
   int statistician::length() const
   {
       return count;
   }
 
   double statistician::sum() const
   {
       return total;
   }
 
   double statistician::mean() const
   {
       return total/count;                     // mean = total divided by the number of numbers
   }
 
   double statistician::minimum() const
   {                                           // minimum = tinyest number
       return tinyest;
   }
 
   double statistician::maximum() const
   {
       return largest;                         // maximum = largest number
   }
 
   bool operator ==(const statistician& s1, const statistician& s2)
   {
       if (s1.length()==s2.length())
	{
    return (1);
    }
    else 
    return (0);
   }
 
   statistician operator + (const statistician & s1, const statistician & s2)
   {
       statistician s3;
       s3.next(s1.sum() + s2.sum());
       return s3;
   }
 
   statistician operator * (const statistician& s1, const statistician& s2)
   {
      statistician s3;
        s3.next(s1.sum() * s2.sum());
       return s3;
   }
}


the last two functions the ones with the overloaded operators dont work right
Last edited on
I apologize if I did something wrong. I tried to be as clear as possible that's why originally I only posted the function that I thought was wrong. My code does compile and run with no errors. The two overloaded operators are not returning anything, hence the title of this thread. I'm sure I implemented them wrong.

The only way to learn programming is to program and I want to learn.
The operators appear to be working as you intended them to work.
http://coliru.stacked-crooked.com/a/3862df2a6dc36723

What is the problem? Perhaps you could post the code in main().
It fails meaning I do not get a new object with the length of s1 + s2.

What do you mean length?
If you mean this length
1
2
3
4
   int statistician::length() const
   {
       return count;
   }

Then it works as it supposed to work, the length() of new object returned by operator "+" or "*" would be 1, no matter what are values of this objects...
If you want to change it, don't use your void next(double r); function.
And could you tell us how exactly should your operators work? Give examples...
Last edited on
Topic archived. No new replies allowed.