我在使用此警告消息时遇到了一些问题,它是在模板容器类中实现的
int k = 0, l = 0;
for ( k =(index+1), l=0; k < sizeC, l < (sizeC-index); k++,l++){
elements[k] = arryCpy[l];
}
delete[] arryCpy;
这是我得到的警告
cont.h: In member function `void Container<T>::insert(T, int)':
cont.h:99: warning: left-hand operand of comma has no effect
cont.h: In member function `void Container<T>::insert(T, int) [with T = double]':
a5testing.cpp:21: instantiated from here
cont.h:99: warning: left-hand operand of comma has no effect
cont.h: In member function `void Container<T>::insert(T, int) [with T = std::string]':
a5testing.cpp:28: instantiated from here
cont.h:99: warning: left-hand operand of comma has no effect
>Exit code: 0
答案 0 :(得分:16)
逗号表达式a,b,c,d,e
与
{
a;
b;
c;
d;
return e;
}
因此,k<sizeC, l<(sizeC - index)
只会返回l < (sizeC - index)
。
要合并条件,请使用&&
或||
。
k < sizeC && l < (sizeC-index) // both must satisfy
k < sizeC || l < (sizeC-index) // either one is fine.
答案 1 :(得分:4)
表达式k < sizeC, l < (sizeC-index)
仅返回右侧测试的结果。使用&&
组合测试:
k < sizeC && l < (sizeC-index)
答案 2 :(得分:2)
更改为:
for ( k =(index+1), l=0; k < sizeC && l < (sizeC-index); k++,l++){
当您评估逗号表达式时,将返回最右侧的参数,以便您:
k < sizeC, l < (sizeC-index)
表达式的计算结果为:
l < (sizeC-index)
因此错过
k < sizeC
使用&&
来组合条件。