C ++ ошибка C2893

0

---Answered → Сделать функции оператора const!

Я пишу шаблон и продолжаю получать следующую ошибку:

Ошибка 1 ошибка C2893: не удалось настроить шаблон функции "неизвестный тип std :: less :: operator() (_ Ty1 &&, _ Ty2 &&) const '

при попытке использовать шаблон с классом, даже если класс имеет перегруженные операторы. Обратите внимание, что шаблон работает с примитивами

    ____TEMPLATE_________

        #include <vector>
        #include <algorithm>
        using namespace std;


    template <class T>

    class Set{
        int elements;
        vector <T> MySet;

    public:

    //error is due to this function

    bool find(const T& value) const
        {
            return binary_search(MySet.begin(), MySet.end(), value);    
        }

_____CLASS_________



class CCustomer{
public:
    int CustomerId;
    string Name;
    string Surname;
    int Phone;

    CCustomer(int ID, string Name, string Surname, int Phone)
{
    this->CustomerId = ID;
    this->Name = Name;
    this->Surname = Surname;
    this->Phone = Phone;
}

    bool operator==(const CCustomer &RHS)
{
    if (this->CustomerId == RHS.CustomerId)
    {
        return true;
    }
    return false;
}
    bool operator<(const CCustomer &RHS)
{
    if (this->CustomerId < RHS.CustomerId)
    {
        return true;
    }
    return false;
}
    bool operator>(const CCustomer &RHS)
{
    if (this->CustomerId > RHS.CustomerId)
    {
        return true;
    }
    return false;
}

};

_____MAIN_______

void main()
{
    CCustomer TEST (1234, "abc", "def", 456);

    Set <CCustomer> Myset;
    Myset.find(Test);
}
  • 2
    Сделайте ваши операторы сравнения const член. И перестаньте говорить, if (b) return true else return false; Просто скажи, return b; ,
  • 1
    Напишите bool operator<(const CCustomer &RHS) const { // ... также для других операторов сравнения. Что такого неясного в этом сообщении об ошибке?
Показать ещё 2 комментария
Теги:
templates
algorithm
search
std

1 ответ

0

Объявить оператор <like

bool operator<(const CCustomer &RHS) const;

Например

bool operator<( const CCustomer &RHS) const
{
    return this->CustomerId < RHS.CustomerId;
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню