overloading operator *, and using private variables

hello this is my first time posting onto this site, and i am having a problem trying to use the private variables in the class.I believe i dont know the proper syntax to use the private part in the class.

1
2
3
4
5
6
7
8
       friend statistician operator *
            (double scale, const statistician & s);
    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
    };

here is part of the class, and the private portion. This is the multiplication operator giving me a cant change the modifiable value error. I tried just multiplying the class and not every variable at a time.
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
statistician operator*(double scale, const statistician & s)
{
		if(s.length() == 0)
		{
			s.count = scale*s.count;
			s.total = scale * s.total;
			s.tinyest = scale * s.tinyest;
			s.largest = scale * s.largest;
			return s;
		}

		if(scale ==0)
		{
			s.count = scale*s.count;
			s.total = scale * s.total;
			s.tinyest = scale * s.tinyest;
		        s.largest = scale * s.largest;
			return s;
		}
		if(scale<0)
		{
			s.count = scale*s.count;
			s.total = scale * s.total;
			s.tinyest = scale * s.largest;
			s.largest = scale * s.tinyest;
			return s;
		}
		if(scale>0)
		{
			s.count = scale * s.count;
			s.total = scale * s.total;
			s.tinyest = scale * s.tinyest;
			s.largest = scale * s.largest;
			return s;
		}
	}


This is what is going to be tested with, and will return 7 if correct
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
int test4( )
{
    // Test program for * operator of the statistician
    // Returns 7 if everything goes okay; otherwise returns 0.

    statistician s, t, u;

    if (s.length( ) || t.length( )) return 0;
    if (s.sum( ) || t.sum( )) return 0;

    u.next(0); u.next(10); u.next(10); u.next(20);

    s = 2*u;
    if (s.length( ) != 4) return 0;
    if (!close(s.sum( ), 80)) return 0;
    if (s.minimum( ) != 0) return 0;
    if (s.maximum( ) != 40) return 0;
    if (!close(s.mean( ), 80.0/4)) return 0;

    s = -2*u;
    if (s.length( ) != 4) return 0;
    if (!close(s.sum( ), -80)) return 0;
    if (s.minimum( ) != -40) return 0;
    if (s.maximum( ) != 0) return 0;
    if (!close(s.mean( ), -80.0/4)) return 0;

    s = 0*u;
    if (s.length( ) != 4) return 0;
    if (!close(s.sum( ), 0)) return 0;
    if (s.minimum( ) != 0) return 0;
    if (s.maximum( ) != 0) return 0;
    if (!close(s.mean( ), 0)) return 0;

    s = 10 * t;
    if (s.length( ) != 0) return 0;
    if (s.sum( ) != 0) return 0;

    return 7;
}
The problem isn't with accessing private variables, it's because you're trying to modify a const object:

1
2
3
4
5
statistician operator*(double scale, const statistician & s)  // <- s is const
{
		if(s.length() == 0)
		{
			s.count = scale*s.count;  // <- so you can't modify it here! 


You wouldn't want to modify s here anyway, since that would have weird side effects. The * operator typically creates another object, modifies that, and returns it:

1
2
3
4
5
6
7
8
9
10
statistician operator*(double scale, const statistician & s)
{
		statistician ret;
		if(s.length() == 0)
		{
			ret.count = scale*s.count;
			ret.total = scale * s.total;
  //...
		return ret;
}
Topic archived. No new replies allowed.