سؤال

Please help me reason out for the error I am getting with this statement, in Linux environment.

p_pAphErrorMessage->getSvc()?l_AphUpdateMessage->setSvc(p_pAphErrorMessage->getSvc()):0;
p_pAphErrorMessage->getObj()?l_AphUpdateMessage->setObj(p_pAphErrorMessage->getObj()):0;

This code is successfully compiled in windows but its giving error in Linux enviroment.

src/aph.cpp:7320: error: âl_AphUpdateMessage->AphMessage::setSvc(((AphUpdateMessage*)p_pAphErrorMessage)->AphUpdateMessage::<anonymous>.AphFidValuesMessage::<anonymous>.AphMessage::getSvc())â has type âvoidâ and is not a throw-expression
src/aph.cpp:7321: error: âl_AphUpdateMessage->AphMessage::setObj(((AphUpdateMessage*)p_pAphErrorMessage)->AphUpdateMessage::<anonymous>.AphFidValuesMessage::<anonymous>.AphMessage::getObj())â has type âvoidâ and is not a throw-expression

I investigated this a bit and suspect returning 0; could be the reason for it. Can we use conditional operator in such a way to use it just for if not for else, eg,

c= a?a:/*DO NOTHING*/;

But, this way I am not getting any success in compilation. Any other recommended way to achieve it.

هل كانت مفيدة؟

المحلول

You have to cast the zero to void, like

condition ? expression : (void) 0;

The reason is that both the setSvc and setObj are declared as returning void (as seen in the error messages). This means the result of the expression where you call them is nothing. But the other "false" expression is an expression with a value. So the ternary expression have one branch that doesn't return a value and one branch that does return a value, which is not allowed. Casting the "false" expression to void makes both branches the same.

نصائح أخرى

No, you can't use the ternary operator this way, as it operates with 3 arguments:

expression1 : expression2 ? expression3

But you always can do:

c = a ? a : c;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top