Hi,i tried to write a code to compare the classes with templates but this code is not working.I think i shoul use operators but i don't know how to write it in my code.Can anyone help?
Also references are not always practical types for template parameters.
Example: MIN<STUDENT&>m2; //template type is a reference.
That would mean that in the STUDENT class:
1 2 3 4 5 6 7 8 9 10 11
template < STUDENT & > //*****with STUDENT& as the parameter type
class MIN
{
//****When the compiler tries to instantiate the class - You will have the following line:
STUDENT & var; //Problem here - see explanation below
public:
X GetMin(X a,X b)
{
return a<b ? a:b;
}
};
When a class declaration contains a reference variable - the compiler will refuse to
make a default constructor - for obvious reasons. References have to be initialized in an initialization list.
So just do: MIN<STUDENT>m2;
Also you will need the overloased < (less than) operator for the STUDENT class - as already suggested by naraku9333
That problem is due to this line (in the MIN CLASS): X var;
This is because the STUDENT class has a constructor that takes parameter - which
means that in order to do STUDENT var you need a default constructor.
If you provide a constructor that take parameters then you (the programmer) has to provide
a default constructor as the compiler won't do it for you.
So add a default constructor to the STUDENT class.
Then that will leave the operator < overload function for the STUDENT to be done.