Hi,

i have some issues with class inheritance and i do not know how to solve it, maybe someone could help me with that ?

//pentathlete.h
#include <iostream>

using namespace std;

template < class T >

class pentathlete {
	protected :
		T comp_res [ 5 ] ;
	/*private:
		int sum_res ;*/
	public :
		class error_1 { } ;
		class error_2 { } ;
	    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 ( isdigit ( comp_res [ i ] ) == false ) throw error_1 ( ) ;
				if ( comp_res [ i ] > 100 ) throw error_2 ( ) ;
			}
		}
		void results ( ) {
			sum_res = 0 ;
			for ( int i = 0; i < 5; i ++ ) {
				sum_res += comp_res [ i ] ;
			}
		}
		void output ( ) { 
			cout << "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 operator + ( pentathlete sec_comp ) {
				if ( sum_res < 0 ) {
					results ( ) ;
				}
				sec_comp . results ( ) ;
				return pentathlete( 0, 0, 0, 0 ,0, 
					sum_res + sec_comp . sum_res ) ;
			}
};
int main ( ) {
	//pentathlete p1, p2, p3, result ;
	pentathlete_team p1, p2, p3, result ;
	cout << "First pentathlete results: \n" ;
	try {
		p1 . input ( ) ;
	}
	catch ( pentathlete_team :: error_1 ) {
		cout << "you entered not a number !\n" ;
		cout << "bye bye\n" ; 
		return 0;
	}
	catch ( pentathlete_team :: error_2 ) {
		cout << "OMG, too big result !\n"
				<< "Maximum possible value:\n"
				<< "100\n" << "bye bye\n" ;
	}
	cout << "Second pentathlete results: \n" ;
	try {
		p2 . input ( ) ;
	}
	catch ( pentathlete_team :: error_1 ) {
		cout << "you entered not a number !\n" ;
		cout << "bye bye\n" ; 
		return 0;
	}
	catch ( pentathlete_team :: error_2 ) {
		cout << "OMG, too big result !\n"
				<< "Maximum possible value:\n"
				<< "100\n" << "bye bye\n" ;
	}
	cout << "Third pentathlete results: \n" ;
	try {
		p3 . input ( ) ;
	}
	catch ( pentathlete_team :: error_1 ) {
		cout << "you entered not a number !\n" ;
		cout << "bye bye\n" ; 
		return 0;
	}
	catch ( pentathlete_team :: error_2 ) {
		cout << "OMG, too big result !\n"
				<< "Maximum possible value:\n"
				<< "100\n" << "bye bye\n" ;
                return 0 ;
	}
	cout << "Summarum results " ;
	result = p1 + p2 + p3 ;
	result . output ( ) ;
	return 0 ;
}

And the error is:

In function 'int main()':
66:21: error: no match for 'operator+' in 'p1.pentathlete_team::operator+(p2.pentathlete_team::<anonymous>) + p3'

If you want your addition operator to take a pentathlete_team, it should take a pentathlete_team and return one too. As so:

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 ) ;
			}

Notice also that you should pass by const-reference, and mark the member function as const as well. Also, you are going to need a constructor similar to that of pentathlete to construct the result.

Frankly, I have no idea why you are using inheritance here. There really isn't any reasonable "is a" relationship between pentathlete and pentathlete_team.

Yes it is absolutely no logical reason for inheritance, but you know, university needs to show me how that works ;) Thank you for your answer !

Edited 5 Years Ago by n0de: n/a

This question has already been answered. Start a new discussion instead.