C ++错误C2893 [英] C++ error C2893
本文介绍了C ++错误C2893的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
---回答! - >使操作功能常量
我写一个模板,并不断收到以下错误:
错误1错误C2893:无法专注函数模板未知类型的std ::较少::运算符()(_ TY1和放大器;&功放;,_ TY2和放大器;&放大器;)常量
尝试使用模板类,即使类已经重载运算符时。注意,模板确实与基元的工作
____TEMPLATE_________
#包括<载体>
#包括<算法>
使用名字空间std;
模板<类T>
类集合{
诠释要素;
矢量< T> MYSET;
上市:
//错误是由于这个功能
布尔发现(const的T&安培;价值)常量
{
返回binary_search的(MySet.begin(),MySet.end(),价值);
}
_____类_________
类CCustomer {
上市:
诠释客户ID;
字符串名称;
字符串姓氏;
INT电话;
CCustomer(INT ID,字符串名称,字符串姓氏,INT电话)
{
这 - >客户ID = ID;
这 - >名称=名称;
这 - >姓=姓氏;
这 - >电话=电话;
}
布尔运算符==(常量CCustomer和放大器; RHS)
{
如果(这 - >客户ID == RHS.CustomerId)
{
返回true;
}
返回false;
}
布尔运算符<(常量CCustomer和放大器; RHS)
{
如果(这 - >客户ID< RHS.CustomerId)
{
返回true;
}
返回false;
}
布尔运算符>(常量CCustomer和放大器; RHS)
{
如果(这 - >客户ID> RHS.CustomerId)
{
返回true;
}
返回false;
}
};
_____主要_______
无效的主要()
{
CCustomer试验(1234,ABC,DEF,456);
设置< CCustomer> MYSET;
Myset.find(试验);
}
解决方案
声明运营商的LT;像
布尔运算符<(常量CCustomer和放大器; RHS)常量;
例如
布尔运算符<(常量CCustomer和放大器; RHS)常量
{
回到这 - >客户ID< RHS.CustomerId;
}
---Answered --> Make the operator functions const!
I am writing a template and keep getting the following error:
Error 1 error C2893: Failed to specialize function template 'unknown-type std::less::operator ()(_Ty1 &&,_Ty2 &&) const'
when trying to use the template with a class, even if the class has overloaded operators. Note that the template does work with primitives
____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);
}
解决方案
Declare operator < like
bool operator<(const CCustomer &RHS) const;
For example
bool operator<( const CCustomer &RHS) const
{
return this->CustomerId < RHS.CustomerId;
}
这篇关于C ++错误C2893的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文