2010-05-15 23 views
7
我遇到一些麻烦,此警告消息

,它是一个模板容器类逗号的左侧操作数没有作用?

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 

回答

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. 
2

更改为:

for (k =(index+1), l=0; k < sizeC && l < (sizeC-index); k++,l++){ 

当你有一个逗号表达式被评估时,最右边的参数被返回,所以你的:

k < sizeC, l < (sizeC-index) 

表达式评估为:

l < (sizeC-index) 

并因此错过

k < sizeC 

使用&&的条件,而不是结合起来。

4

表达式k < sizeC, l < (sizeC-index)只返回右侧测试的结果。使用&&结合试验:

k < sizeC && l < (sizeC-index)