重载模板运算符调用单独的类运算符

  • 本文关键字:运算符 单独 调用 重载 c++
  • 更新时间 :
  • 英文 :


我有一个包含其他类优先级队列的模板类,我需要使用优先级重载器调用单个类重载器以根据各个类的首选项进行比较(在这种情况下是年龄,在另一个类中可能是价格。

我绝对毫不怀疑我已经实现了不正确的运算符重载,因此将不胜感激。

例如

#include <iostream>
#include <queue>
#include <string>
using namespace std;    
class Animal {
public:
Animal();
Animal(string t, int a);
int get_age()const;
bool operator< ( Animal& b) const;
void display()const;
private:
string type;
double age;
};
void Animal::display() const
{
cout << "Type: " << type << "    Age: " << age;
}
int Animal::get_age() const
{
return age;
}
Animal::Animal(){}
Animal::Animal(string t, int a)
{
type = t;
age = a;
}
bool Animal::operator< ( Animal& b) const
{
return b.get_age();
}
template<typename T>
class Collection {
public:
Collection();
Collection(string n, string d);
void add_item(const T& c); 
private:
priority_queue <T> pets;
string name; // Name of the collection
string description; // Descriptions of the collection
};
template<typename T>
Collection<T>::Collection(){}
template<typename T>
Collection<T>::Collection(string n, string d)
{
name = n;
description = d;
}
template<typename T>
bool operator<(const T& one, const T& two) 
{
return one.operator<(two);
}
template<typename T>
void Collection<T>::add_item(const T& c)
{
pets.push(c);
}
int main(){
Animal p1("Dog", 10);
Animal p2("Cat", 5);
Animal p3("Turtle", 24);
Collection<Animal> P("Pets", "My Pets");
P.add_item(p1);
P.add_item(p2);
P.add_item(p3);
cout << endl;
return 0;
}

我收到此错误,但不确定需要做什么才能修复它。我必须将类重载器保留为单个变量(Animal&b(。

task.cpp:在实例化 'bool operator<(const T&, const T&( [与 T = 动物]': C:\mingw-4.7.1\bin../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_function.h:237:22: 从 'bool std::less<_Tp>::operator(((const _Tp&, const _Tp&( 中要求 常量 [带 _Tp = 动物]' C:\mingw-4.7.1\bin../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_heap.h:310:4:从'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare( [_RandomAccessIterator = __gnu_cxx::__normal_iterator>>; _Distance = int; _Tp = 动物; _Compare = 标准::更少]' C:\mingw-4.7.1\bin../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_heap.h:442:4:从'void std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare( [_RandomAccessIterator = __gnu_cxx::__normal_iterator>>; _Compare = std::less]' C:\mingw-4.7.1\bin../lib/gcc/mingw32/4.7.1/include/c++/bits/stl_queue.h:393:9:从'std::p riority_queue<_Tp, _Sequence, _Compare>::p riority_queue(const _Compare&, const _Sequence&( [with _Tp = Animal; _Sequence = std::vector>; _Compare = std::less]' task.cpp:57:45: 必需的 'Collection::Collection(std::string, std::string( [with T = Animal; std::string = std::basic_string]' 任务.cpp:79:43: 必需 从此处任务.cpp:66:30:错误:没有用于调用的匹配函数 "动物::操作员<(const Animal&( const"任务.cpp:66:30:注意: 候选项是:任务.cpp:36:6:注意:布尔动物::操作员<(动物&( const 任务.cpp:36:6:注意:参数 1 没有已知的转换 "const Animal"到"Animal&"任务.cpp:在函数"bool"中 operator<(const T&, const T&( [with T = Animal]':

您的比较

bool Animal::operator< ( Animal& b) const
{
return b.get_age();      // returns true always unless age == 0 
}

没有比较,它应该采用const参数。你应该有类似的东西

bool Animal::operator< (const Animal& b) const 
// ^----------------------- const !
{
return get_age() < b.get_age();
}

顺便说一句,您不需要为优先级队列使用成员operator<。特别是如果您想以不同的方式对对象进行排序,我建议您不要使用它,而是将 lambda 传递给priority_queue。有关示例,请参阅此处。

<的两个重载都是有问题的

bool Animal::operator< ( Animal& b) const

Animal也应该const.您还需要比较这两个参数,否则期望<提供排序的内容(例如您的priority_queue(将具有未定义的行为。

您不使用任何非公开的Animal,所以我建议您将其更改为

bool operator< (const Animal & lhs, const Animal & rhs)
{ return lhs.get_age() < rhs.get_age(); } 

这样做的好处是一视同仁,而不是隐含地对待一方。

template<typename T>
bool operator<(const T& one, const T& two) 
{
return one.operator<(two);
}

此模板匹配所有类型完全是多余的a < b可以呼叫会员或免费operator <。只需删除此模板即可。

相关内容

最新更新