【问题标题】:Error with max and min in a template class模板类中的最大值和最小值错误
【发布时间】:2015-04-20 01:51:15
【问题描述】:

我编写了一个名为 Triple 的模板类来获取 type T 的 3 个对象,并且我有三个函数来获取这 3 个对象的最小值、最大值和中值。

我正在使用 STL 中的 minmax 函数来完成此任务。 我必须将所有方法设为 const 才能使 max 和 min 起作用,并且它确实适用于普通类型,但后来我创建了一个名为 Employee 的类,这个类有 2 个函数 getSalary()getName(),我已经重载了 < > 运算符,所以我可以在我的模板中使用这个类。

这是我遇到问题的地方,编译器抱怨: const std::string Employee::getName(void)' : cannot convert 'this' pointer from 'const Employee' to 'Employee &'

我尝试将员工类中的所有内容都更改为 const,但没有成功,除非一切都是 const,否则我无法让三重类工作。

我哪里出错了?

三重.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

三重.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));
}

员工.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;
};

员工.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);
}

【问题讨论】:

  • 不是(还)你的问题,而是stackoverflow.com/questions/495021/…
  • 注意,C++11以后可以直接写std::max({firstElement, secondElement, thirdElement});
  • 我没有看到任何 const 方法,只有一堆 const 参数和通常无意义的 const 返回值。一个 const 方法看起来像 void foo() const {}

标签: c++ templates stl max min


【解决方案1】:

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

要么标记getName() const(当你的函数不需要修改对象时总是一个好主意),或者只是从Triple&lt;T&gt;::median()Triple&lt;T&gt;::minimum()Triple&lt;T&gt;::maximum()通过非常量值返回。

【讨论】:

    【解决方案2】:

    尝试将 const 添加到您的 重载运算符:

    在 Employee.cpp 中:

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

    在 Employee.h 中

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

    【讨论】:

    • 这是我为解决问题所做的,感谢@vsoftco 的回答,在您发布此问题之前我已经弄清楚了,我误解了我应该如何在函数上实现 const,我很惊讶直到现在才意识到我做错了。
    猜你喜欢
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 2019-01-02
    • 2015-12-01
    • 2023-04-02
    相关资源
    最近更新 更多