Results 1 to 6 of 6

Thread: Invalid conversion in ternary statement

  1. #1
    Join Date
    Dec 2011
    Location
    Living in Spain 100km North of Valencia
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Invalid conversion in ternary statement

    I have discovered something with a ternary statement that I don't quite understand.
    If I use a normal if - else construct I have no problem in compiling, however using the same variables in a ternary statement I get a Invalid conversion from int to constant char* error.

    Here is my code:

    Header file

    Qt Code:
    1. #ifndef TERNARYTEST_H
    2. #define TERNARYTEST_H
    3. #include <QString>
    4. #include <QDebug>
    5. #include <QRegExp>
    6.  
    7. typedef QMap<QString, QVariant> MyMap;
    8.  
    9. void setup(void);
    10. bool isInt(QString);
    11.  
    12. #endif // TERNARYTEST_H
    To copy to clipboard, switch view to plain text mode 

    and the main file

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <ternaryTest.h>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7. setup();
    8. return a.exec();
    9. }
    10.  
    11. void setup()
    12. {
    13. MyMap map;
    14.  
    15. QString str = "Release Time: 2000";
    16. QString val;
    17. QString key;
    18. bool ok;
    19. val = (str.section(':',1,-1)).trimmed();
    20. key = (str.section(':',0,0)).toUpper().trimmed();
    21. qDebug()<<val;
    22. qDebug()<<key;
    23.  
    24. if ( isInt(val) )
    25. map[key] = val.toInt();
    26. else
    27. map[key] = val;
    28.  
    29. qDebug()<<map;
    30.  
    31. map[key] = isInt(val) ? val.toInt(&ok, 10) : val; /// Error compiling here
    32.  
    33.  
    34. exit(0);
    35. }
    36.  
    37. bool isInt(QString s)
    38. {
    39. QRegExp re("\\d*");
    40. return re.exactMatch(s);
    41. }
    To copy to clipboard, switch view to plain text mode 

    As the if - else constructs works fine I have no problem. Just curious as to the compiler error on the ternary statement.

    The object of the program is to use everything before the first occurrence of a colon as a key in a QMap and everything after, including any colons as a QVariant value (Int or QString).

    Thanks in advance.
    Regards
    Graham Benney

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Invalid conversion in ternary statement

    The ternary operator requires that both "branches" have the same type.
    In your example that is not the case, you have "int" in one branch and "QString" in the other.

    You can wrap both statements in QVariant() or QVariant::fromValue() to have both branches of type "QVariant"

    Btw, I guess this is just an example, as this wouldn't make much sense as it would be very wasteful.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Invalid conversion in ternary statement

    The object of the program is to use everything before the first occurrence of a colon as a key in a QMap and everything after, including any colons as a QVariant value (Int or QString).
    This makes no sense at all. Any string that starts with a colon will never be parsed as "int" and will fail any attempt to convert to anything other than a string. Parsing of any string containing an embedded colon will stop as soon as the colon is reached, so if the left side of the colon contains something that can be interpreted as an integer or floating point number it will be returned as such, and anything to the right will be discarded. Any string containing a non-digit character (other than '+' or ' -') will stop parsing at the non digit character if you try to convert it to int.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Dec 2011
    Location
    Living in Spain 100km North of Valencia
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Invalid conversion in ternary statement

    anda_skoa
    You're absolutely right of course. Should have seen that. Thank you.
    G


    Added after 7 minutes:


    d_stranz
    Thank you. I know that in this case there will always be characters before the first colon. (The data comes from a python script written by me.)
    You're right, of course, should it start with a colon all hell would be let loose so thanks for pointing that out.
    Regards
    G
    Last edited by GrahamB; 8th June 2016 at 08:04.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Invalid conversion in ternary statement

    Wouldn't it be much better to let toInt() decide if it is an int and then already have the value for free instead of first checking with a regex and then basically parsing the int again?

    Cheers,
    _

  6. #6
    Join Date
    Dec 2011
    Location
    Living in Spain 100km North of Valencia
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Invalid conversion in ternary statement

    Yes, much better. Thanks
    Regards
    G

Similar Threads

  1. Invalid conversion from ‘char*’ to ‘char’
    By Atomic_Sheep in forum General Programming
    Replies: 4
    Last Post: 16th January 2015, 11:20
  2. IF-Statement problem
    By ayanda83 in forum Newbie
    Replies: 2
    Last Post: 18th November 2013, 08:02
  3. Replies: 4
    Last Post: 18th May 2012, 06:04
  4. Invalid conversion error while Storing into Combobox
    By owais_blore in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 16:08
  5. How to get value from a query statement ?
    By hohoanganh205 in forum Newbie
    Replies: 3
    Last Post: 5th January 2012, 14:23

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
  •  
Qt is a trademark of The Qt Company.