Hi,
after making some adjustmens to my code as recommended, I get those discard qualifiers errors..
//pentathlete.h
#include <iostream>
using namespace std;
template < class T >
class pentathlete {
protected :
T comp_res [ 5 ] ;
public :
T sum_res ;
pentathlete ( ) {
for ( int i = 0 ; i < 5 ; i ++ ) {
comp_res [ i ] ;
}
sum_res = -1 ;
}
pentathlete ( int fir, int sec,
int thi, int fou, int fif, int fin ) {
comp_res [ 0 ] = fir ;
comp_res [ 1 ] = sec ;
comp_res [ 2 ] = thi ;
comp_res [ 3 ] = fou ;
comp_res [ 4 ] = fif ;
sum_res = fin ;
}
void input ( ) {
for ( int i = 0 ; i < 5 ; i ++ ) {
cout << "Please enter " << i + 1
<< " competition results: " ;
cin >> comp_res [ i ] ;
if ( comp_res [ i ] > 100 ) throw "Out of range !\n" ;
if ( cin . fail ( ) ) throw "Bad input !\n" ;
cin . clear ( ) ;
}
}
void results ( ) {
sum_res = 0 ;
for ( int i = 0; i < 5; i ++ ) {
sum_res += comp_res [ i ] ;
}
}
void output ( ) {
cout << "summarum results: " << sum_res << "\n" ;
}
};
//main program file
#include <iostream>
#include <C:\MinGW\msys\1.0\home\Owner\pentathlete.h>
//
using namespace std ;
//
class pentathlete_team: public pentathlete < int > {
public:
pentathlete_team ( ) : pentathlete < int > ( ) { }
pentathlete_team ( int fir, int sec,
int thi, int fou, int fif, int fin ) : pentathlete < int > ( fir, sec, thi, fou, fif, fin ) {
}
pentathlete_team operator + ( const pentathlete_team & sec_comp ) const {
if ( sum_res < 0 ) {
results ( ) ;
}
sec_comp . results ( ) ;
return pentathlete_team ( 0, 0, 0, 0 ,0,
sum_res + sec_comp . sum_res ) ;
}
};
int main ( ) {
pentathlete_team p1, p2, p3, result ;
cout << "First pentathlete results: \n" ;
try {
p1 . input ( ) ;
}
catch ( const char * error ) {
cout << "Exception raised: " << error ;
return 0 ;
}
catch ( const char * error ) {
cout << "Exception raised: " << error ;
return 0;
}
cout << "Second pentathlete results: \n" ;
try {
p2 . input ( ) ;
}
catch ( const char * error ) {
cout << "Exception raised: " << error ;
return 0 ;
}
catch ( const char * error ) {
cout << "Exception raised: " << error ;
return 0 ;
}
cout << "Third pentathlete results: \n" ;
try {
p3 . input ( ) ;
return 0 ;
}
catch ( const char * error ) {
cout << "Exception raised: " << error ;
return 0 ;
}
catch ( const char * error ) {
cout << "Exception raised: " << error ;
return 0 ;
}
cout << "Summarum results " ;
result = p1 + p2 + p3 ;
result . output ( ) ;
return 0 ;
}
// g++ output
: In member function 'pentathlete_team pentathlete_team::operator+(const pentathlete_team&) const':
:16:16: error: passing 'const pentathlete_team' as 'this' argument of 'void pentathlete<T>::results() [with T = int]' discards qualifiers
:18:26: error: passing 'const pentathlete_team' as 'this' argument of 'void pentathlete<T>::results() [with T = int]' discards qualifiers
if i remove those "const" from operator overloading function - it compiles, but it does not work - operator overloading function does not seem to be initialized somehow..