...

View Full Version : C++ (boolean)? do if true : do if false;



sankou
01-30-2012, 11:13 PM
I'm having an issue in one of my functions. I'm trying to format my if statement using the ? and : because I thinks it looks nicer and makes things more readable. But I keep getting an error.

This is the code:


if(m_search != m_first->prev)
m_search = m_search->next;
else
return;


I want it to look like this:


(m_search != m_first->prev) ? m_search = m_search->next : return;


However I get the following error.
1>c:...\projects\program1\linklist.h(155): error C2059: syntax error : 'return'

Are you not allowed to return when using that format? Or am I just writing it wrong?

Oh, and it's a void function by the way.

sankou
01-31-2012, 12:31 AM
anyone have a resource on how to format if statements like that?

oracleguy
01-31-2012, 01:00 AM
You can't put a return call into a ternary operation. You can put them before but not in the middle of.

sankou
01-31-2012, 01:01 AM
After lots of searching, turns out thats called the ternary operator. And it wont work because it's expecting an expression whereas return is just a statement.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum