Thread: Conditional operator: second operand is neither a throw-expression nor of type void

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    14

    Conditional operator: second operand is neither a throw-expression nor of type void

    hello,
    have this piece of code and wonder why void funtion is causing the problem:
    Code:
    void TurnLed( int flag){
        if(flag){
            digitalWrite(PinLed, HIGH);
            } else {
                digitalWrite(PinLed, LOW);
            }
        }
    
      void loop() {
      Serial.println(digitalRead(PinBtn));
    
      (state == digitalRead(PinBtn)) ? : (state=digitalRead(PinBtn) , TurnLed(state) ) ;
    
      delay(300);               // wait for a second
     }
    Code:
    /home/az/work/Arduino/Sketches/Examples/Push_Led/Push_Led.ino:35:82: error: third operand to the conditional operator is of type 'void', but the second operand is neither a throw-expression nor of type 'void'
       (state == digitalRead(PinBtn)) ? : (state=digitalRead(PinBtn) , TurnLed(state) ) ;
                                                                                      ^
    
    [Stino - Exit with error code 1.]

  2. #2
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Hi,
    The second and third operand of a ? : operator need to have the same type. Because there is no second operand the compiler can't deduce the type of the operand.
    Try to rewrite the statement as an if/then/else expression.

    Cheers

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,122
    Code:
    Serial.println(digitalRead(PinBtn));
    In addition, this is a C forum, and the code you posted is C++.

  4. #4
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    Quote Originally Posted by talahin View Post
    Hi,
    The second and third operand of a ? : operator need to have the same type. Because there is no second operand the compiler can't deduce the type of the operand.
    Try to rewrite the statement as an if/then/else expression.

    Cheers
    i've been trying to get the conditional operator to work. Always used "if/then".

    (state == digitalRead(PinBtn)) ? : (state=digitalRead(PinBtn) , (void)TurnLed(state) ) ;
    nor
    (state == digitalRead(PinBtn)) ? void : (void)(state=digitalRead(PinBtn) , TurnLed(state) ) ;

    make any difference.

  5. #5
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    Quote Originally Posted by rstanley View Post
    Code:
    Serial.println(digitalRead(PinBtn));
    In addition, this is a C forum, and the code you posted is C++.
    yeah, you are right of-course. Just thought that there is no difference between implementations in these two languages.

  6. #6
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Hi,
    Try
    Code:
    if (state != digitalRead(PinBtn)) {
        state = digitalRead(PinBtn) , TurnLed(state) ;
    }
    Also you're reading the button 3 times in your loop. What happens if the button is pressed or released between reading the button states.

    Cheers

  7. #7
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    Quote Originally Posted by talahin View Post
    Hi,
    Try
    Code:
    if (state != digitalRead(PinBtn)) {
        state = digitalRead(PinBtn) , TurnLed(state) ;
    }
    Also you're reading the button 3 times in your loop. What happens if the button is pressed or released between reading the button states.

    Cheers
    oh, yes, lousy code - was trying to get the "?" to work.

    your code makes compiler happy
    thank you. So , no way to twist the "?" operator?

    And, BTW, why it takes "," in :
    Code:
    state = digitalRead(PinBtn) , TurnLed(state)
    it also takes a ";"..
    Last edited by babo; 10-19-2015 at 09:03 PM.

  8. #8
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Hi,
    thank you. So , no way to twist the "?" operator?
    First it's not the "?" operator, but "? :" operator. It's a ternary operator, meaning it needs 3 operands.
    As you noticed, leaving out one of the operands will make the compiler complain.


    Code:
    state = digitalRead(PinBtn) , TurnLed(state);
    The comma character in your statement is an operator not a seperator. It will first evaluate the statement on the left side of the comma, drop the result and then evaluate the right side and use the result of that. This is why it was complaining in your original code, because the result type of TurnLed() is void.

    Cheers

  9. #9
    Registered User
    Join Date
    Sep 2015
    Posts
    14
    Quote Originally Posted by talahin View Post
    Hi,

    The comma character in your statement is an operator not a seperator. It will first evaluate the statement on the left side of the comma, drop the result and then evaluate the right side and use the result of that. This is why it was complaining in your original code, because the result type of TurnLed() is void.

    Cheers
    i did not know that. Thank you!

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,122
    Quote Originally Posted by babo View Post
    yeah, you are right of-course. Just thought that there is no difference between implementations in these two languages.
    There is no difference in the use of the ternary operator in C and C++, but since you have not provided the entire code, you might have other questions or problems that would be better asked in the C++ forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conditional Operator
    By Waqas Asad in forum C Programming
    Replies: 4
    Last Post: 12-19-2011, 09:11 AM
  2. conditional operator
    By nasser in forum C Programming
    Replies: 2
    Last Post: 10-23-2011, 07:18 PM
  3. DDD conditional breakpoint : expression error
    By lamko in forum C Programming
    Replies: 9
    Last Post: 07-29-2011, 10:12 AM
  4. Conditional Operator
    By markcocoa10 in forum C Programming
    Replies: 7
    Last Post: 10-15-2010, 06:22 PM
  5. illegal operand with & operator
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2004, 08:26 AM

Tags for this Thread