【发布时间】:2015-04-20 01:51:15
【问题描述】:
我编写了一个名为 Triple 的模板类来获取 type T 的 3 个对象,并且我有三个函数来获取这 3 个对象的最小值、最大值和中值。
我正在使用 STL 中的 min 和 max 函数来完成此任务。
我必须将所有方法设为 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 {}