PDA

View Full Version : Invalid conversion in ternary statement



GrahamB
5th June 2016, 09:52
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


#ifndef TERNARYTEST_H
#define TERNARYTEST_H
#include <QString>
#include <QDebug>
#include <QRegExp>

typedef QMap<QString, QVariant> MyMap;

void setup(void);
bool isInt(QString);

#endif // TERNARYTEST_H

and the main file


#include <QCoreApplication>
#include <ternaryTest.h>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
setup();
return a.exec();
}

void setup()
{
MyMap map;

QString str = "Release Time: 2000";
QString val;
QString key;
bool ok;
val = (str.section(':',1,-1)).trimmed();
key = (str.section(':',0,0)).toUpper().trimmed();
qDebug()<<val;
qDebug()<<key;

if ( isInt(val) )
map[key] = val.toInt();
else
map[key] = val;

qDebug()<<map;

map[key] = isInt(val) ? val.toInt(&ok, 10) : val; /// Error compiling here


exit(0);
}

bool isInt(QString s)
{
QRegExp re("\\d*");
return re.exactMatch(s);
}


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

anda_skoa
5th June 2016, 21:16
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,
_

d_stranz
5th June 2016, 22:10
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.

GrahamB
8th June 2016, 08:04
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

anda_skoa
8th June 2016, 19:36
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,
_

GrahamB
9th June 2016, 08:23
Yes, much better. Thanks
Regards
G