C++ error C2893

C++ error C2893

本文关键字:C2893 error C++      更新时间:2023-10-16

---已回答 --> 使运算符函数常量!

我正在编写模板并不断收到以下错误:

错误

1 错误 C2893:无法专用化函数模板"未知类型 std::less::运算符 ()(_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);
}

声明运算符<</p>

bool operator<(const CCustomer &RHS) const;

例如

bool operator<( const CCustomer &RHS) const
{
    return this->CustomerId < RHS.CustomerId;
}
相关文章: