2013-05-09 40 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; 
{ 
}