---已回答 - >使操作符函数为常量!
我正在编写模板并不断收到以下错误:
错误1错误C2893:无法专门化功能模板' unknown-type 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);
}
答案 0 :(得分:0)
声明运营商&lt;像
bool operator<(const CCustomer &RHS) const;
例如
bool operator<( const CCustomer &RHS) const
{
return this->CustomerId < RHS.CustomerId;
}