模板类中的max和min出错

JTK*_*JTK 0 c++ templates stl max min

我写了一个模板类,调用Triple3个对象,type T我有三个函数来获取这3个对象的最小值,最大值和中值.

我正在使用STL 的minmax函数来实现这一目标.我必须使我的所有方法const得到最大值和最小值才能工作,并且它确实有效,使用普通类型,但后来我创建了一个名为的类Employee,这个类有2个函数getSalary(),而且getName(),我重载了< >运算符,所以我可以在我的模板中使用此类.

这是我遇到问题的地方,编译器抱怨:const std :: string Employee :: getName(void)':无法将'this'指针从'const Employee'转换为'Employee&'

我已经尝试将所有内容更改为员工类中的c​​onst而没有成功,除非一切都是常量,否则我无法使三元组工作.

我哪里出错了?

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
Run Code Online (Sandbox Code Playgroud)

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));
}
Run Code Online (Sandbox Code Playgroud)

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;
};
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

vso*_*tco 7

threeTriplet.maximum()返回一个const Employee对象.然后你调用它Employee::getName()(它没有标记const)函数,因此编译器会抱怨,因为你不允许在const对象上调用非const成员函数.

无论是纪念getName() const(总是一个好主意,当你的功能并不需要修改的对象),或只返回Triple<T>::median(),Triple<T>::minimum(),Triple<T>::maximum()通过非const值.