Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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 11:16 PM.

  • #2
    New Coder
    Join Date
    Oct 2011
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    anyone have a resource on how to format if statements like that?

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    You can't put a return call into a ternary operation. You can put them before but not in the middle of.
    OracleGuy

  • #4
    New Coder
    Join Date
    Oct 2011
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •