2013-07-20 30 views
2

你能告诉我有什么问题?:运算符,它告诉错误:操作逗号在C++?:条件

C2446: ':' : no conversion from 'int' to 'std::basic_ostream<_Elem,_Traits>' 
      c:\documents\visual studio 2005\projects\8.14\8.14\8.14.cpp 36 

验证码:

int _tmain(int argc, _TCHAR* argv[]) 
{ 
int B; 
int A=(6,B=8); 
bool c = true; 
cout << endl << B; 
while (B != 100) 
{ 
cout << "qgkdf\n"; 
(A<B) ? (c = 100, B=100, cout << "!!!") : (A = 100); 
A--; 
} 
_getch(); 
return 0; 
} 
+0

我认为','不是一个运算符和行int A =(6,B = 8);'不正确 –

+5

@ABFORCE它是一个运算符。 – hvd

+0

@ hvd:可以超负荷吗? –

回答

3

的类型2个操作数的有条件的操作符需要相同。

(A<B) ? (c = 100, B=100, cout << "!!!") : (A = 100); 

类型的c = 100, B=100, cout << "!!!"cout << "!!!"的类型,这是std::ostream

A = 100的类型是int

这两种类型不匹配,因此错误。

编辑:逗号运算符返回最后一部分的值。例如,您不能添加int:

(A<B) ? (c = 100, B=100, (cout << "!!!"), 42) : (A = 100); 
//          ^^^^ 

Live example here

+0

确定我的错误 –

+0

(A thomas

+0

@thomas这也是一个解决方案。由于OP似乎想要对代码进行混淆,因此我不会选择哪一个; – Synxis

2

如果你打算写混淆代码,请确保您知道如何使用强制类型转换,作为解决方案显然是把结果的cout << "!!!"int

(A<B) ? (c = 100, B=100, reinterpret_cast<int>(cout << "!!!")) : (A = 100); 
1

由于返回值不被使用它可能会更清楚地让双方都失效。
虽然不如使用一个好的旧“如果”一样清晰。

1

这是公然滥用?:操作符。使用if语句。这就是他们的目的。