2010-05-15 114 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) 
相關問題