C++ 错误C2893未能专门化函数模板';未知类型std::等于<;void>;::运算符()(&u-Ty1&;、&u-Ty2&;)无异常(<;expr>;)const&&x27;
我使用模板创建了两个类,分别称为C++ 错误C2893未能专门化函数模板';未知类型std::等于<;void>;::运算符()(&u-Ty1&;、&u-Ty2&;)无异常(<;expr>;)const&&x27;,c++,C++,我使用模板创建了两个类,分别称为bag和linked\u list。我作为bag类的成员编写了一个名为find的函数。此函数用于查找bag类中元素的位置 我已经重载了类ThreeD和链表的操作符==。我声明一个元素指针item*p,并且find函数正确运行 但是当涉及到另一个元素时,程序crush出现了一个错误2893。为什么会发生这种情况 ThreeDclass: template <class T> class ThreeD { public: T ht; T w
bag
和linked\u list
。我作为bag类的成员编写了一个名为find
的函数。此函数用于查找bag类中元素的位置
我已经重载了类ThreeD
和链表的操作符==
。我声明一个元素指针item*p
,并且find
函数正确运行
但是当涉及到另一个元素时,程序crush出现了一个错误2893。为什么会发生这种情况
ThreeD
class:
template <class T> class ThreeD {
public:
T ht;
T wid;
T dep;
ThreeD() { ht = wid = dep = 0; }
ThreeD(T i) { ht = wid = dep = i; }
ThreeD(T a, T b, T c) { ht = a; wid = b; dep = c; }
T getVol() { return ht * wid * dep; }
//two objects are equal if their getVol() return the same value.
bool operator==(ThreeD<T>& t1) {
if (getVol() == t1.getVol()) {
return true;
}
else { return false; }
}
bool operator!=(ThreeD<T>& t1) {
if (getVol() == t1.getVol()) {
return false;
}
else { return true; }
}
};
bool operator==(linked_list& L) {
node<T>* it1 = head;
node<T>* it2 = L.head;
int count1 = 0;
while (it1 != nullptr) {
count1++;
it1 = it1->next;
}
int count2 = 0;
while (it2 != nullptr) {
count2++;
it2 = it2->next;
}
if (count1 != count2) { return false; }
else {
it1 = head;
it2 = L.head;
while (it1 != nullptr && it2 != nullptr) {
if (it1->value != it2->value) { return false; }
it1 = it1->next;
it2 = it2->next;
}
return true;
}
}
main
功能:
int main(){
item<linked_list<ThreeD<double>>>* p2;
p2 = BLTD.find(LTD1);
item<list<linked_list<ThreeD<int>>>>* p20 = B16.find(V4);
if (p20 != nullptr) cout << (p20->data).size() << endl;
}
intmain(){
项目*p2;
p2=BLTD.find(LTD1);
项目*p20=B16.查找(V4);
if(p20!=nullptr)cout data).size()欢迎使用堆栈溢出。请花时间浏览并参考中的材料,了解您可以在此处询问的内容和方式。特别重要的是发布一个。将bool操作符==(ThreeD&t1)
更改为bool操作符==(const ThreeD&t1)常量
,类似地,对于!=
它也可以工作。因此有必要将运算符函数保持为常量?