首页 > 解决方案 > 重载模板运算符调用单独的类运算符

问题描述

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

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

例如

#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&) [with T = Animal]' 的实例化中: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&) const [with _Tp = Animal]' 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 = std::less]' 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) [with _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: :priority_queue<_Tp, _Sequence, _Compare>::priority_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]' task.cpp:79:43: 从这里需要 task.cpp:66:30: 错误:没有匹配函数调用'Animal::operator<(const Animal&) const' task.cpp:66:30: 注意: 候选是: task.cpp:36:6: 注意: bool Animal::operator<(Animal&) const task.cpp:36:6: 注意: 参数 1 的未知转换来自'const Animal' 到 'Animal&

标签: c++

解决方案


你的比较

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. 例如,请参见此处的示例。


推荐阅读