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

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;

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.

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

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.

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.