I can't figure out what this error is saying.

Error:

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp||In function 'bool operator==(const statistic&, const statistic&)':|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|225|error: passing 'const statistic' as 'this' argument of 'float statistic::sum()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|225|error: passing 'const statistic' as 'this' argument of 'float statistic::sum()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|226|error: passing 'const statistic' as 'this' argument of 'int statistic::length()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|226|error: passing 'const statistic' as 'this' argument of 'int statistic::length()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|227|error: passing 'const statistic' as 'this' argument of 'float statistic::max()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|227|error: passing 'const statistic' as 'this' argument of 'float statistic::max()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|228|error: passing 'const statistic' as 'this' argument of 'float statistic::min()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|228|error: passing 'const statistic' as 'this' argument of 'float statistic::min()' discards qualifiers|

||=== Build finished: 8 errors, 0 warnings ===|

Problem with the code starts in the Bool operator ==, function towards the bottom.

```
// Kyle Bennett
// CS 372
// Throttle Class
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
ofstream outfile;
class statistic
{
public:
statistic();
void fresh();
void next(float);
int length();
float max();
float min();
float sum();
float mean();
float last();
void display();
friend statistic operator +(const statistic &stat1, const statistic &stat2);
friend ostream& operator <<(ostream& output, const statistic& stat2);
private:
float avg;
float high;
float low;
float total;
int amount;
float end;
};
bool operator ==(statistic const &stat1, statistic const &stat2);
bool operator !=(statistic const &stat1, statistic const &stat2);
int main()
{
outfile.open("statout.out"); //open outfile
if (!outfile) // check outfile
{
outfile << "Output file did not open.\n";
return 1;
}
statistic stat1;
stat1.next(5.5);
stat1.next(6.6);
stat1.next(8.8);
stat1.next(-3.4);
stat1.next(-0.5);
stat1.next(4.7);
stat1.next(9.1);
stat1.display();
stat1.next(5.2);
stat1.next(-3.3);
stat1.next(-8.5);
stat1.next(3.2);
stat1.next(5.5);
stat1.display();
outfile << "Starting a new sequence.\n";
stat1.fresh();
stat1.display();
outfile << "Entering into Sequence 1.\n";
stat1.next(301);
stat1.next(211);
stat1.next(981);
stat1.next(487);
stat1.next(342);
stat1.next(235);
stat1.next(224);
stat1.next(663);
stat1.next(424);
stat1.next(434);
stat1.display();
statistic stat2;
outfile << "Entering into Sequence 2.\n";
stat2.next(103);
stat2.next(821);
stat2.next(871);
stat2.next(487);
stat2.next(312);
stat2.next(245);
stat2.next(224);
stat2.next(623);
stat2.next(424);
stat2.next(432);
stat2.display();
if (stat1 == stat2)
{
outfile << "The two sequences are equal.\n\n";
}
else
{
outfile << "The two sequences are not equal.\n\n";
}
if (stat1 != stat2)
{
outfile << "The two sequences are not equal.\n\n";
}
else
{
outfile << "The two sequences are equal.\n\n";
}
statistic stat3;
outfile << "Adding the two sequences.\n";
stat3 = stat1 + stat2;
outfile << stat3;
return 0;
}
statistic::statistic()
{
total = 0;
amount = 0;
high = 0;
low = 0;
}
void statistic::fresh()
{
total = 0;
amount = 0;
high = 0;
low = 0;
}
void statistic::next(float x)
{
end = x;
total += x;
amount += 1;
if (end < low || low == 0)
{
low = end;
}
if (end > high || high == 0)
{
high = end;
}
}
int statistic::length()
{
return amount;
}
float statistic::max()
{
return high;
}
float statistic::min()
{
return low;
}
float statistic::sum()
{
return total;
}
float statistic::mean()
{
avg = total / amount;
return avg;
}
float statistic::last()
{
return end;
}
void statistic::display()
{
outfile << "The length of the sequence is: " << amount << endl;
outfile << "The sum of all the numbers is: " << total << endl;
if (amount > 0)
{
outfile << "The last number entered was: " << end << endl;
outfile << "The highest value is: " << high;
outfile << "\nThe lowest value is: " << low << endl;
outfile << "The arithmetic mean of the numbers is: " << mean() << endl;
}
else
{
outfile << "The sequence is empty. There for there is no last ";
outfile << "number, max/min, or \narithmetic mean.\n";
}
outfile << "\n\n";
}
bool operator==(const statistic &stat1, const statistic &stat2)
{
return (stat1.sum() == stat2.sum())
&& (stat1.length() == stat2.length())
&& (stat1.max() == stat2.max())
&& (stat1.min() == stat2.min());
}
statistic operator +(const statistic& stat1, const statistic& stat2)
{
statistic n_stat;
n_stat.total = stat1.total + stat2.total;
n_stat.amount = stat1.amount + stat2.amount;
if (stat1.high > stat2.high)
{
n_stat.high = stat1.high;
}
else
{
n_stat.high = stat2.high;
}
if (stat1.low < stat2.low)
{
n_stat.low = stat1.low;
}
else
{
n_stat.low = stat2.low;
}
n_stat.avg = n_stat.total / n_stat.amount;
return n_stat;
}
bool operator !=(statistic const &stat1, statistic const &stat2)
{
return !(stat1 == stat2);
}
ostream& operator <<(ostream& output, const statistic& stat3)
{
output << "The length of the two sequences are: " << stat3.amount << endl;
output << "The sum of all the numbers is: " << stat3.total << endl;
output << "The highest value is: " << stat3.high << endl;
output << "The lowest value is: " << stat3.low << endl;
output << "The arithmetic mean of the two sequences are: " << stat3.avg << endl;
return output;
}
```

Please help me.

You are calling a non-const function by a const object. Hence the error. Make the functions as const and you'll get rid of this error.

```
int statistic::length() const
{
return amount;
}
```

There seems to be another solution to this. I haven't tried it and I seriously believe it is NOT a reliable solution. Use const_cast on your object before calling those non-const functions.

You are calling a non-const function by a const object. Hence the error. Make the functions as const and you'll get rid of this error.

`int statistic::length() const { return amount; }`

This is a good practice in general--if your member functions don't change the object, declare them `const`

.

There seems to be another solution to this. I haven't tried it and I seriously believe it is NOT a reliable solution. Use const_cast on your object before calling those non-const functions.

Yeah, don't do that. There are legitimate uses for `const_cast`

, but you may live your entire life without needing it.

I can't figure out what this error is saying.

Error:

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp||In function 'bool operator==(const statistic&, const statistic&)':|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|225|error: passing 'const statistic' as 'this' argument of 'float statistic::sum()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|225|error: passing 'const statistic' as 'this' argument of 'float statistic::sum()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|226|error: passing 'const statistic' as 'this' argument of 'int statistic::length()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|226|error: passing 'const statistic' as 'this' argument of 'int statistic::length()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|227|error: passing 'const statistic' as 'this' argument of 'float statistic::max()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|227|error: passing 'const statistic' as 'this' argument of 'float statistic::max()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|228|error: passing 'const statistic' as 'this' argument of 'float statistic::min()' discards qualifiers|

C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|228|error: passing 'const statistic' as 'this' argument of 'float statistic::min()' discards qualifiers|

||=== Build finished: 8 errors, 0 warnings ===|Problem with the code starts in the Bool operator ==, function towards the bottom.

`// Kyle Bennett // CS 372 // Throttle Class #include <iostream> #include <cstdlib> #include <fstream> using namespace std; ofstream outfile; class statistic { public: statistic(); void fresh(); void next(float); int length(); float max(); float min(); float sum(); float mean(); float last(); void display(); friend statistic operator +(const statistic &stat1, const statistic &stat2); friend ostream& operator <<(ostream& output, const statistic& stat2); private: float avg; float high; float low; float total; int amount; float end; }; bool operator ==(statistic const &stat1, statistic const &stat2); bool operator !=(statistic const &stat1, statistic const &stat2); int main() { outfile.open("statout.out"); //open outfile if (!outfile) // check outfile { outfile << "Output file did not open.\n"; return 1; } statistic stat1; stat1.next(5.5); stat1.next(6.6); stat1.next(8.8); stat1.next(-3.4); stat1.next(-0.5); stat1.next(4.7); stat1.next(9.1); stat1.display(); stat1.next(5.2); stat1.next(-3.3); stat1.next(-8.5); stat1.next(3.2); stat1.next(5.5); stat1.display(); outfile << "Starting a new sequence.\n"; stat1.fresh(); stat1.display(); outfile << "Entering into Sequence 1.\n"; stat1.next(301); stat1.next(211); stat1.next(981); stat1.next(487); stat1.next(342); stat1.next(235); stat1.next(224); stat1.next(663); stat1.next(424); stat1.next(434); stat1.display(); statistic stat2; outfile << "Entering into Sequence 2.\n"; stat2.next(103); stat2.next(821); stat2.next(871); stat2.next(487); stat2.next(312); stat2.next(245); stat2.next(224); stat2.next(623); stat2.next(424); stat2.next(432); stat2.display(); if (stat1 == stat2) { outfile << "The two sequences are equal.\n\n"; } else { outfile << "The two sequences are not equal.\n\n"; } if (stat1 != stat2) { outfile << "The two sequences are not equal.\n\n"; } else { outfile << "The two sequences are equal.\n\n"; } statistic stat3; outfile << "Adding the two sequences.\n"; stat3 = stat1 + stat2; outfile << stat3; return 0; } statistic::statistic() { total = 0; amount = 0; high = 0; low = 0; } void statistic::fresh() { total = 0; amount = 0; high = 0; low = 0; } void statistic::next(float x) { end = x; total += x; amount += 1; if (end < low || low == 0) { low = end; } if (end > high || high == 0) { high = end; } } int statistic::length() { return amount; } float statistic::max() { return high; } float statistic::min() { return low; } float statistic::sum() { return total; } float statistic::mean() { avg = total / amount; return avg; } float statistic::last() { return end; } void statistic::display() { outfile << "The length of the sequence is: " << amount << endl; outfile << "The sum of all the numbers is: " << total << endl; if (amount > 0) { outfile << "The last number entered was: " << end << endl; outfile << "The highest value is: " << high; outfile << "\nThe lowest value is: " << low << endl; outfile << "The arithmetic mean of the numbers is: " << mean() << endl; } else { outfile << "The sequence is empty. There for there is no last "; outfile << "number, max/min, or \narithmetic mean.\n"; } outfile << "\n\n"; } bool operator==(const statistic &stat1, const statistic &stat2) { return (stat1.sum() == stat2.sum()) && (stat1.length() == stat2.length()) && (stat1.max() == stat2.max()) && (stat1.min() == stat2.min()); } statistic operator +(const statistic& stat1, const statistic& stat2) { statistic n_stat; n_stat.total = stat1.total + stat2.total; n_stat.amount = stat1.amount + stat2.amount; if (stat1.high > stat2.high) { n_stat.high = stat1.high; } else { n_stat.high = stat2.high; } if (stat1.low < stat2.low) { n_stat.low = stat1.low; } else { n_stat.low = stat2.low; } n_stat.avg = n_stat.total / n_stat.amount; return n_stat; } bool operator !=(statistic const &stat1, statistic const &stat2) { return !(stat1 == stat2); } ostream& operator <<(ostream& output, const statistic& stat3) { output << "The length of the two sequences are: " << stat3.amount << endl; output << "The sum of all the numbers is: " << stat3.total << endl; output << "The highest value is: " << stat3.high << endl; output << "The lowest value is: " << stat3.low << endl; output << "The arithmetic mean of the two sequences are: " << stat3.avg << endl; return output; }`

Please help me.

Hi

Actually for methods like

`int length();`

,

`float max () ;`

and the min , last... you should have two version one const and the other normal one.

For example;

```
int length()const ;
int length();
```

you should have two version one const and the other normal one.

For example;`int length()const ; int length();`

What does this get you that you don't already have with `int length() const;`

? The `const`

lets you use `length()`

when you have a `const`

reference to an instance of your class, but it doesn't prevent anything when you don't. Or am I missing something?