user3050215
user3050215

Reputation: 197

Switch case refusing to recognize constant as initialized

I'm making a function in Qt that is going to print out different things depending on a key it receives.

I have stored the keys to compare with in a separate headerfile, in a separate namespace called reference. The switch statement only wants to "see" the uninitialized declaration of the key and therefore thinks that I'm comparing with a non-constant value. However I'm not, as the value really is initialized in the namespace's source file. And I off course cannot initialize my keys in the header, as that will create multiple definitions.

How do I "tell" my switch-case that I actually have initialized the keys I'm comparing with?

void MainWindow::printOuts(char key, float value)
{

    const int test1 =2;
    const int test2 = (const int)reference::potentiometer;
    const char test3= reference::potentiometer;
     const char test4 =0;

 //const char potentiometer lies in a namespace called reference


    if(key==reference::potentiometer)
    {//works
        qDebug()<<"Got a potentiometer value!!";

    }

    switch (key)
    {
        case test1: //Works ok
        case test2: //gives "cannot appear in a constant-expression"
        case test3: ////gives "cannot appear in a constant-expression"
        case test4: //Works ok
    }


}

Upvotes: 2

Views: 131

Answers (4)

RobbieE
RobbieE

Reputation: 4360

As many of the other contributors have said, you cannot have a case where the value is not known precisely at the time of compilation.

test1 is known; it's 2. test4 is known; it's 0

If your code needs to be written following the same algorithm, you can substitute your entire switch statement with a cascading if statement like so:

if (key == test1) {
   // ...
} else if (key == test2) {
   // ...
} else if (key == test3) {
   // ...
} else if (key == test4) {
   // ...
} else {
   // default code goes here
}

Upvotes: 1

You should be able to define the const in a different header(I tried it and it worked), but potentiometer needs to be defined as a const, not typecast as a const. In your current code, even though test 2 and test 3 are consts, if potentiometer is not a const, then test cases 2 and 3 could have different values each time you entered this method. With your current code you'll run into another error because you've used the same case twice(test2 and test3).

(The main reason switch statements don't allow variable comparisons is so they can be optimized at compilation.)

Upvotes: 0

user2428400
user2428400

Reputation:

You're only allowed to use compile-time constants as the potential values in a a switch statement. Your second and third values are determined at runtime because they are defined in a different compilation unit.

Upvotes: 1

reference::potentiometer needs to be defined in this compilation unit to be a constant expression, and not just some constant with external linkage. Define its value in the header it is declared, and this will work fine.

Upvotes: 1

Related Questions