JTK
JTK

Reputation: 1519

Error with max and min in a template class

I have written a template class called Triple to take 3 objects of type T and I have three functions to get the min, max and median of these 3 objects.

I'm using the min and max functions from the STL to accomplish this. I had to make all my methods const to get the max and min to work and it does work, with normal types, but then I created a class called Employee, this class has 2 functions getSalary() and the getName(), I'v overloaded the < > operators so I can use this class in my template.

This is where I run in to problems, the compiler is complaining with: const std::string Employee::getName(void)' : cannot convert 'this' pointer from 'const Employee' to 'Employee &'

I've tried changing everything to const in the employee class without success, and I cant get the triple class to work unless everything is const.

Where have I gone wrong?

Triple.h

#ifndef TRIPLE_H
#define TRIPLE_H
template<class T>
class Triple{
public:
    Triple();
    Triple(const T fE, const T sE, const T tE);
    const T maximum();
    const T minimum();
    const T median();
private:
    const T firstElement;
    const T secondElement;
    const T thirdElement;


};
#endif

Triple.CPP

#include "Triple.h"
#include <algorithm> 
using std::max;
using std::min;

template<class T>
Triple<T>::Triple(){

}
template<class T>
Triple<T>::Triple(const T fE, const T sE, const T tE)
    :firstElement(fE), secondElement(sE), thirdElement(tE){
}
template<class T>
 const T Triple<T>::maximum(){

     return max(max(firstElement, secondElement), thirdElement);
}
template<class T>
const T Triple<T>::minimum(){

    return min(min(firstElement, secondElement), thirdElement);
}
template<class T>
const T Triple<T>::median(){

    return max(min(firstElement, secondElement), min(max(firstElement, secondElement), thirdElement));
}

Employee.h

#include <string>
#include <iostream>
using  std::ostream;
using std::string;
class Employee{
public:
    Employee();
    Employee(string,  double);
    const string getName();
    double getSalary();
    bool Employee::operator<(const Employee &rop);
    bool Employee::operator>(const Employee &rop);

private:
    double salary;
    string name;
};

Employee.cpp

#include "Employee.h"

Employee::Employee(){

}
Employee::Employee(string nameIn, double salaryIn)
    :name(nameIn), salary(salaryIn) {
};
const string Employee::getName(){
    return name;
}
double Employee::getSalary(){
    return salary;
}
bool Employee::operator < (const Employee &rop)
{
    return (salary < rop.salary);
}
bool Employee::operator >(const Employee &rop)
{
    return (salary > rop.salary);
}

main.cpp

#include "Triple.h"
#include "Triple.cpp"
#include "Employee.h"
#include <iostream>
using std::cout;
using std::endl;

int main(){

     Employee john("John", 70000);
     Employee tom("Tom", 30000);
     Employee mark("Mark", 10000);


    Triple<int> oneTriplet(1, 2, 3);
    Triple<char> twoTriplet('A', 'B', 'C');
    Triple<Employee> threeTriplet(john, tom, mark);


    cout << oneTriplet.maximum() << endl;
    cout << oneTriplet.minimum() << endl;
    cout << oneTriplet.median() << endl;

    cout << twoTriplet.maximum() << endl;
    cout << twoTriplet.minimum() << endl;
    cout << twoTriplet.median() << endl;

    cout << threeTriplet.maximum().getName() << endl;
    cout << threeTriplet.minimum().getName() << endl;



    system("pause");
    return(0);
}

Upvotes: 0

Views: 1006

Answers (2)

Daniofb
Daniofb

Reputation: 71

Try to add const to your <> overload operators:

in Employee.cpp:

bool Employee::operator < (const Employee &rop) const
{
    return (salary < rop.salary);
}

bool Employee::operator < (const Employee &rop) const
{
    return (salary < rop.salary);
}

in Employee.h

bool Employee::operator<(const Employee &rop)const;
bool Employee::operator>(const Employee &rop)const;

Upvotes: 1

vsoftco
vsoftco

Reputation: 56547

threeTriplet.maximum() returns a const Employee object. Then you invoke the Employee::getName() (which is not marked const) function on it, so the compiler complains, since you are not allow to invoke non-const member functions on const objects.

Either mark the getName() const (always a good idea when your function doesn't need to modify the object), or just return from Triple<T>::median(), Triple<T>::minimum(), Triple<T>::maximum() by non-const value.

Upvotes: 7

Related Questions