Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2012, 10:13 PM   PM User | #1
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
Question C++ (boolean)? do if true : do if false;

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:
Code:
if(m_search != m_first->prev)
     m_search = m_search->next;
else
     return;
I want it to look like this:
Code:
(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.

Last edited by sankou; 01-30-2012 at 10:16 PM..
sankou is offline   Reply With Quote
Old 01-30-2012, 11:31 PM   PM User | #2
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
anyone have a resource on how to format if statements like that?
sankou is offline   Reply With Quote
Old 01-31-2012, 12:00 AM   PM User | #3
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,042
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
You can't put a return call into a ternary operation. You can put them before but not in the middle of.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 01-31-2012, 12:01 AM   PM User | #4
sankou
New Coder

 
Join Date: Oct 2011
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
sankou is an unknown quantity at this point
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.
sankou is offline   Reply With Quote
Reply

Bookmarks

Tags
boolean, c++, expression, if format

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:00 PM.


Advertisement
Log in to turn off these ads.