PDA

View Full Version : [SOLVED] question about the if statement c++



hakermania
June 16th, 2011, 05:18 PM
Hello, I am trying to do the following:


QString arg="X";
int a=1;
if((arg=="X" || arg=="Y) && a==1)
do_action();
I want to say: if arg is (X or Y) and a is 1, then do_action();
and I get error message:


error: no match for ‘operator||’ in ‘((arg.QString::operator==(((const char*)"X")) || arg.QString::operator==(((const char*)"Y"))) || arg’
Is this something Qt-dependent or my code is wrong?

gusnan
June 16th, 2011, 05:24 PM
try adding some parenthesis around the elements in that line:

if(((arg=="X") || (arg=="Y)) && (a==1))

hakermania
June 16th, 2011, 05:38 PM
No, same problem....
The actual if statement is (modified after your suggestion)


if( ((first_argument=="--start") || (first_argument=="--pause") || (first_argument=="--stop") || (first_argument="--next") || (first_argument="--previous")) && (QCoreApplication::arguments().count()>2) )

amauk
June 16th, 2011, 05:45 PM
missing double-equals on 2 of the comparisons


first_argument=="--next"
first_argument=="--previous"

hakermania
June 16th, 2011, 05:55 PM
missing double-equals on 2 of the comparisons


first_argument=="--next"
first_argument=="--previous"

nice thanks :)
I've done this one million times without an error at all :| I assumed they were corert :)

amauk
June 16th, 2011, 06:06 PM
Value assignment & value comparison are fundamentally different, so make sure you're using the correct one


// If foo equals 1
if (foo == 1)Vs.
// If it's possible to assign
// the value 1 to variable foo
// (most likely always true,
// unless foo is immutable)
if (foo = 1)