Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to match an int to predefined variable



  • I have these variable declarations on my mainwindow.h file
    They have integers from 0 to 5.
    Im reading my json file, and assign them this integers.

    int TM_AP_ID_PID;    //it is 0 and  i use it as an idex
    int TM_PAR_ROLL_KP;
    int TM_PAR_ROLL_KI;
    int TM_PAR_ROLL_KD;
    int TM_PAR_PITCH_KP;
    int TM_PAR_PITCH_KI;
    
    

    I have another header file and in there I have these predefines.

    #define TM_AP_ID_PID       	    0   //i used them as an index of variables.   
    #define TM_PAR_ROLL_KP      1
    #define TM_PAR_ROLL_KI        2
    #define TM_PAR_ROLL_KD      3
    #define TM_PAR_PITCH_KP     4
    #define TM_PAR_PITCH_KI      5
    

    I want to match my integer definitions to my predefines.
    How can i do it, is it possible to do?
    I'm open to any idea


  • Lifetime Qt Champion

    @suslucoder You could use a QMap to map your definitions to actual values:

    QMap<int, int> vars;
    ...
    vars[TM_AP_ID_PID] = 0;
    


  • @suslucoder Use enumerator rather than #define variables



  • @suslucoder
    It is not clear what you are trying to do here.

    I want to match my integer definitions to my predefines.

    "Match" in what sense?

    You have 6 variables. Does that mean each of those variables can take its own value, independent of whatever is in the others? In that case, it is not clear what the purpose of the #defines is?

    Especially when you write:

    int TM_AP_ID_PID;    //it is 0 and  i use it as an idex
    

    Is the value of TM_AP_ID_PID always 0? Then why do you have it in a variable?

    Purely at a guess, you should be considering one or both of these:

    • Use a C++ enum.

    • Create a 6-element array (or map) for your currently separate variables, so that you can address them via array[TM_something].


Log in to reply