0

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);
}
JTK
  • 1,469
  • 2
  • 22
  • 39
  • 2
    Not (yet) your problem, but http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Baum mit Augen Feb 18 '15 at 21:24
  • 3
    Note that since C++11, you may directly write `std::max({firstElement, secondElement, thirdElement});` – Jarod42 Feb 18 '15 at 21:24
  • 1
    I don't see any const methods, just a bunch of const parameters and generally meaningless const return values. A const method looks like `void foo() const {}` – Retired Ninja Feb 18 '15 at 21:26

2 Answers2

7

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.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
1

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;
Daniofb
  • 71
  • 6
  • This is what I did to fix the problem, I'd figured it out before you posted this thanks to @vsoftco answer, I misunderstood how I should implement const on a function, I'm surprised it took me till now to realize I was doing this wrong. – JTK Feb 18 '15 at 22:39