Results 1 to 5 of 5

Thread: error: invalid conversion from ‘int’ to ‘InitializationState

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default error: invalid conversion from ‘int’ to ‘InitializationState

    I got to add some C code to my qt object n it's important not to change the C code at all.
    qt notifies the following error:

    error: invalid conversion from ‘int’ to ‘InitializationState’
    when the C code tries to OR an element of the enum type with it's variable.

    Here's the description:

    Qt Code:
    1. enum InitializationState {
    2.  
    3. INIT_NO_ONE = 0x00,
    4. INIT_NO_TWO = 0x01,
    5. INIT_NO_THREE = 0x02,
    6. INIT_NO_FOUR = 0x04
    7. };
    8.  
    9. typedef enum InitializationState InitializationState_t;
    10.  
    11. InitializationState_t InitState = INIT_NO_ONE;
    To copy to clipboard, switch view to plain text mode 

    the errors I receive pop up one by one whenever I try to OR:
    Qt Code:
    1. InitState |= INIT_NO_ONE;
    To copy to clipboard, switch view to plain text mode 

    I also tried accessing the elements of enum using . sign, though it failed.
    any helps would be appreciable : )

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: error: invalid conversion from ‘int’ to ‘InitializationState

    Casting like this should work:

    InitState = static_cast<InitializationState>(InitState | INIT_NO_ONE);

  3. #3
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: error: invalid conversion from ‘int’ to ‘InitializationState

    Or just:
    Qt Code:
    1. InitState = InitializationState(InitState | INIT_NO_ONE);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: error: invalid conversion from ‘int’ to ‘InitializationState

    Or even easier: use QFlags.

    Qt Code:
    1. typedef enum InitializationState
    2. {
    3.  
    4. INIT_NO_ONE = 0x00,
    5. INIT_NO_TWO = 0x01,
    6. INIT_NO_THREE = 0x02,
    7. INIT_NO_FOUR = 0x04
    8. };
    9.  
    10. Q_DECLARE_FLAGS( InitializationStates, InitializationState );
    11. Q_DECLARE_OPERATORS_FOR_FLAGS( InitializationStates );
    12.  
    13. InitializationStates initState = INIT_NO_ONE; // NOTE: "InitializationStates", not "InitializationState"
    14.  
    15. initState |= INIT_NO_TWO;
    16.  
    17. if ( initState.testFlag( INIT_NO_TWO ) )
    18. {
    19. // INIT_NO_TWO is set.
    20. }
    To copy to clipboard, switch view to plain text mode 

    Then you can AND, OR, and test bits using the QFlags operators with no need to cast anything.
    Last edited by d_stranz; 17th May 2012 at 17:53.

  5. The following user says thank you to d_stranz for this useful post:

    olidem (18th May 2012)

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: error: invalid conversion from ‘int’ to ‘InitializationState

    The original problem is that an enum variable of type InitializationState can only hold one of the allowable enum values at a time. The compiler is telling you no for a reason. Trying to stuff an illegal value into the variable by using an evil cast is not going to end well.

    QFlags is the Qt answer as d_stranz has pointed out.

Similar Threads

  1. Invalid conversion error while Storing into Combobox
    By owais_blore in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 16:08
  2. X Error: BadRequest (invalid request code or no such operation) 1
    By Luc4 in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 18th April 2011, 14:40
  3. Building static Qt. Error 126 invalid argument
    By kea_ in forum Installation and Deployment
    Replies: 2
    Last Post: 20th March 2011, 07:58
  4. error: invalid use of void expression
    By ChineseGeek in forum Qt Programming
    Replies: 17
    Last Post: 3rd October 2009, 08:59
  5. error:invalid use of member
    By quickNitin in forum General Programming
    Replies: 4
    Last Post: 19th June 2006, 15:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.