2013-05-09 71 views
0

我在編寫一個返回1s和0s而不是true或false的代碼。但這似乎並不正確。運算符重載(int as bool)

int Short_Vector::operator==(const Short_Vector& obj){ 
    if(a == obj.a && b == obj.b && c == obj.c && d == obj.d){ 
     return 1; 
    }else{ 
     return 0; 
    } 
} 

所以它應該爲每個變量返回一個值。

我也試過這樣:

int Short_Vector::operator==(const Short_Vector& obj){ 
    int a_tf, b_tf, c_tf, d_tf; 
    if(a == obj.a){ 
     a_tf = 1; 
    }else{ 
     a_tf = 0; 
    } 
    if(b == obj.b){ 
     b_tf = 1; 
    }else{ 
     b_tf = 0; 
    } 
    if(c == obj.c){ 
     c_tf = 1; 
    }else{ 
     c_tf = 0; 
    } 
    if(d == obj.d){ 
     d_tf = 1; 
    }else{ 
     d_tf = 0; 
    } 
    return(a_tf, b_tf, c_tf, d_tf) 
} 

但我得到的逗號是操作錯誤。

EDIT

獲取錯誤:錯誤:從 'INT' 轉換到非標量類型「Short_Vector。

我試圖表示一個看起來像這樣的矢量[9,1,5,5]。

然後我會說

`Short_Vector a(2, 6, 9, 4); 

Short_Vector b(3, 8, 7, 6); 

Short_Vector c = a == b; 

cout<<c;` 

輸出則是:[0,0,0,0]

+0

究竟是你想回來嗎?四位表示?數字1100或類似?四個單獨的值? – chris 2013-05-09 19:19:55

+0

第一個代碼實際上應該工作。它做錯了什麼? – 0x499602D2 2013-05-09 19:21:57

+1

@ 0x499602D2,OP想要返回值中的四件事情(或者至少這是我從中間語句中得到的印象)。 – chris 2013-05-09 19:23:31

回答

2

如果你想有結果爲Short_Vector,試試這個:

Short_Vector Short_Vector::operator==(const Short_Vector& obj) { 
    return Short_Vector(
     a == obj.a, 
     b == obj.b, 
     c == obj.c, 
     d == obj.d 
    ); 
} 
+0

謝謝,但,錯誤:前預期主表達式「(」令牌| 錯誤:預期前主表達式「)」令牌 – 2013-05-09 19:44:45

+0

@Adegoke A對不起,我有一個額外的在末端逗號。 – Detheroc 2013-05-09 19:46:29

+0

謝謝! :-D – 2013-05-09 19:47:28

1

逗號操作將無法正常工作,你設定的方式。它將實際評估其每個操作數並返回最後一個操作數。編者向你提出了一個關於這個小小誤解的警告。

一種替代方法是設置包含數字true/false相當於你的布爾表達式的每一位:

unsigned int n = 0; 

n |= (a == obj.a) << 0; 
n |= (b == obj.b) << 1; 
n |= (c == obj.c) << 2; 
n |= (d == obj.d) << 3; 

return n; 

您可以使用一個較小的數據類型一樣char或者您可以使用std::bitset

3

您可以使用std::bitset設置位爲每個成員

std::bitset<4> Short_Vector::operator==(const Short_Vector& obj){ 
    std::bitset<4> r; 

    r[0] = (a == obj.a); 
    r[1] = (b == obj.b); 
    r[2] = (c == obj.c); 
    r[3] = (d == obj.d); 

    return r; 
} 

的平等,你可以使用它像

Short_Vector a(1,2,3,4); 
Short_Vector b(1,0,3,4); 

std::bitset<4> res = (a==b); 
std::cout << res; 

應該給你

1011 

std::bitset是很好的,因爲它提供了便利的方法,如allanynone(還有更多)。這樣您就可以輕鬆檢查聚合值。

+0

我會寫'R [0] =一== obj.a;'等 – jrok 2013-05-09 19:28:50

+0

@jrok是的是乾淨多了。更新。 – stardust 2013-05-09 19:35:09

1

如果你必須使用一個int作爲返回類型,你可以使用左移位運算符,做一些事情,如:

int result = 0; 
result += a_tf << 3; //Shifts the bit 3 places to the left. 
result += b_tf << 2; //Shifts the bit 2 places to the left. 
result += c_tf << 1; //Shifts the bit 1 place to the left. 
result += d_tf; //Puts d_tf as bit 0 
return result; 

而得到每一個背出使用逐位和:

result = obj1 == obj2; //Where obj1 and 2 are your compared objects 
int a_tf = (result >> 3) & 1; 
int b_tf = (result >> 2) & 1; 
int c_tf = (result >> 1) & 1; 
int d_tf = result & 1; 

雖然我不得不說,使用bitset的Named解決方案更容易理解,插入/檢索單個值更容易。

3

第二種方法不行,因爲返回類型是「廉政」和「(a_tf,b_tf,c_tf, d_tf)'不是一個整數,而是4個整數,用逗號分隔。

既然你想回到4個布爾值,你可以做到以下幾點:

int Short_Vector::operator==(const Short_Vector& obj) 
{ 
    //... 
    return (a_tf) | (b_tf << 1) | (c_tf << 2) | (d_tf << 3); 
} 

//the caller would do the follwoing: 

int result = (MyObject1 == MyObject2); 

if(result & (1 << 1) //b_tf is set to 1; 
{ 
} 
if(result & (1 << 3) //d_tf is set to 1; 
{ 
}