template<typename T> T SmartIO::Peek() {
T temp;
T tempReturn;
while(true){
temp = *(T*)&buffer[ptrSeek];
if(temp !=0){
ptrSeek++;
tempReturn += *(T*)&buffer[ptrSeek];
}
else{
break;
}
}
return tempReturn;
}
所以我想做的是,从 ptrSeek 开始循环读取,将值添加到 temp 并检查 temp 的值是否 !=0 将此值添加到 tempReturn,一旦 temp 的值为 0 就中断循环并返回tempReturnbut,但它一直给我这个错误:
error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
我该如何解决这个问题?
最佳答案
如果您尝试将任意类型 T
与数字常量 0 进行比较,那么您的模板代码存在一个基本问题,您在此处的代码中正在这样做:
T temp;
/* ... */
if(temp != 0){
/* ... */
}
这里的问题是 T
是任意类型(在这种情况下,您似乎是用 std::string
实例化模板),但您期望该类型与 0 相当。这很好 - 它只是将 T
限制为可以与 0 进行比较的类型 - 但事实上你将此报告为错误我是不确定您是否知道这一点。
你的选择要么是不使用 std::string
作为参数来实例化这个模板(按照它的写法,我认为你不应该这样做,因为看起来该函数一直将某种类型的值加在一起),或者调试模板并更改其行为。我不太确定您希望代码做什么,所以在后一种情况下,我不确定我该如何提供帮助。
希望这能澄清事情!
关于c++ - 错误 C2678 : binary '!=' : no operator found which takes a left-hand operand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936409/