没有找到接受左操作数的操作符…(使用类模板)
no operator found which takes a left operand... (using a class template)
我正在重载类模板的操作符,我得到了错误:错误1错误C2678: binary '>':没有找到左操作数类型为'const CSet <的操作符;int>的
CSet.h:
bool CSet<T>::operator>(const CSet<T>& mySet) const{
bool flag = false;
for (int i = 0; i < size; i++){
for (int j = 0; j < mySet.size; j++){
if (arr[i] == mySet[j]){
flag = true;
break;
}
if (j == mySet.size - 1 && flag == false)
return false;
}
}
return true;
}
void CSet<T>::operator+=(T& myVal){
T* temp = new T[size + 1];
for (int i = 0; i < size; i++){
if (arr[i] == myVal){
delete[] temp;
return;
}
}
for (int i = 0; i < size; i++)
temp[i] = arr[i];
delete[] arr;
arr = temp;
arr[size] = myVal;
}
mian.cpp:
#include "Set.h"
int main(){
CSet<int> mySet, yourSet;
mySet += 3; mySet += 4; mySet += 5;
yourSet += 5; yourSet += 4; yourSet += 3;
bool boom;
boom = mySet == yourSet;
}
现在,从错误中我了解到编译器不能将T转换为int。我的问题是:为什么不呢?这不就是模板的全部目的吗?
一定有什么东西我错过了,因为这没有意义(至少对我来说)。
您很可能想要/需要将其更改为:
bool CSet<T>::operator>(const CSet<T>& mySet) const {
// ... ^^^^^ Note addition here.
当你调用这样的成员函数时:
if (x > y) ...
等于:if (x.operator>(y))
。参数类型中的"const
"表示右操作数可以为const
。要允许左操作数为const
,您可以在上面所示的位置添加const
。
相关文章:
- 控制允许动态运行c++的并发操作数
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- C++ 带有向量的 IF 计算操作数
- 是否所有C++运算符都接受操作数?
- 一元*运算符的操作数是否期望一个 prvalue
- 与'operator='不匹配(操作数类型'String'且"void")
- 不正确的操作数类型 MSVC
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 如何使操作符的左操作数<<一个函数
- 右操作数为负时位移位操作符的行为
- 操作符重载,二进制表达式的操作数无效
- 重载逗号操作符真的会影响其操作数的求值顺序吗?
- 将右值引用作为左操作数重载加法操作符被认为是一种良好的做法
- 多个操作符匹配这些操作数
- 操作符重载-没有匹配的' Operator != '(操作数类型为
- 重载插入操作符:没有找到接受右操作数'unsigned int'(或者没有可接受的转换)
- c++,通过指针传递泛型数组,继承,错误:没有接受右操作数的操作符
- 没有找到接受左操作数的操作符…(使用类模板)
- c++中为一个操作数重载二进制操作符