Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C4189, why?
Qt 6.11 is out! See what's new in the release blog

C4189, why?

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 6 Posters 2.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have cleaned and rebuilt this project a couple of times just to see if this still comes up, I am getting:

    C4189: 'pTruth': local variable is initialised but not referenced
    

    The function this is occurring on is:

    void truth::setEnabled(bool blnEnable)
    {
        if ( truth::mspSpinMax != nullptr )
        {
            truth::mspSpinMax->setEnabled(blnEnable);
        }
        if ( truth::mspSpinMin != nullptr )
        {
            truth::mspSpinMin->setEnabled(blnEnable);
        }
        if ( truth::mspSlider != nullptr )
        {
            truth::mspSlider->setEnabled(blnEnable);
            truth::mspSlider->setVisible(blnEnable);
        }
        foreach( truth* pTruth, truth::mslstAllTruths )
        {
            pTruth->setEnabled(false);
        }
    }
    

    The yellow warning icon appears on the foreach line, clearly pTruth is used in the loop, so why is this warning being displayed?

    setEnabled public static function, here is the prototype:

    static void setEnabled(bool blnEnable);
    

    I am using Qt 5.9.2 with MSVC2015 on Windows 10.

    Kind Regards,
    Sy

    KroMignonK J.HilkJ 2 Replies Last reply
    0
    • SPlattenS SPlatten

      I have cleaned and rebuilt this project a couple of times just to see if this still comes up, I am getting:

      C4189: 'pTruth': local variable is initialised but not referenced
      

      The function this is occurring on is:

      void truth::setEnabled(bool blnEnable)
      {
          if ( truth::mspSpinMax != nullptr )
          {
              truth::mspSpinMax->setEnabled(blnEnable);
          }
          if ( truth::mspSpinMin != nullptr )
          {
              truth::mspSpinMin->setEnabled(blnEnable);
          }
          if ( truth::mspSlider != nullptr )
          {
              truth::mspSlider->setEnabled(blnEnable);
              truth::mspSlider->setVisible(blnEnable);
          }
          foreach( truth* pTruth, truth::mslstAllTruths )
          {
              pTruth->setEnabled(false);
          }
      }
      

      The yellow warning icon appears on the foreach line, clearly pTruth is used in the loop, so why is this warning being displayed?

      setEnabled public static function, here is the prototype:

      static void setEnabled(bool blnEnable);
      

      I am using Qt 5.9.2 with MSVC2015 on Windows 10.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #6

      @SPlatten said in C4189, why?:

      foreach

      also, please use the standard range for loop instead of foreach/Q_FOREACH there are reasons, why its deprecated and I believe even removed from Qt6 :D


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      sierdzioS 1 Reply Last reply
      3
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Because setEnabled() is a static function - no need for pTruth.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        SPlattenS 1 Reply Last reply
        2
        • SPlattenS SPlatten

          I have cleaned and rebuilt this project a couple of times just to see if this still comes up, I am getting:

          C4189: 'pTruth': local variable is initialised but not referenced
          

          The function this is occurring on is:

          void truth::setEnabled(bool blnEnable)
          {
              if ( truth::mspSpinMax != nullptr )
              {
                  truth::mspSpinMax->setEnabled(blnEnable);
              }
              if ( truth::mspSpinMin != nullptr )
              {
                  truth::mspSpinMin->setEnabled(blnEnable);
              }
              if ( truth::mspSlider != nullptr )
              {
                  truth::mspSlider->setEnabled(blnEnable);
                  truth::mspSlider->setVisible(blnEnable);
              }
              foreach( truth* pTruth, truth::mslstAllTruths )
              {
                  pTruth->setEnabled(false);
              }
          }
          

          The yellow warning icon appears on the foreach line, clearly pTruth is used in the loop, so why is this warning being displayed?

          setEnabled public static function, here is the prototype:

          static void setEnabled(bool blnEnable);
          

          I am using Qt 5.9.2 with MSVC2015 on Windows 10.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #3

          @SPlatten said in C4189, why?:

          The yellow warning icon appears on the foreach line, clearly pTruth is used in the loop, so why is this warning being displayed?
          setEnabled public static function, here is the prototype:
          static void setEnabled(bool blnEnable);

          Why do you use a foreach loop to got access to a static method?
          You don't need class instances to access static methods. I guess this is why you get this warning... But I am not a C++ expert, I may be wrong.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          SPlattenS 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            Because setEnabled() is a static function - no need for pTruth.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #4

            @Christian-Ehrlicher , that doesn't make sense because in the function it is iterating through a list and clearly using the pointer.

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • KroMignonK KroMignon

              @SPlatten said in C4189, why?:

              The yellow warning icon appears on the foreach line, clearly pTruth is used in the loop, so why is this warning being displayed?
              setEnabled public static function, here is the prototype:
              static void setEnabled(bool blnEnable);

              Why do you use a foreach loop to got access to a static method?
              You don't need class instances to access static methods. I guess this is why you get this warning... But I am not a C++ expert, I may be wrong.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by SPlatten
              #5

              @KroMignon, @Christian-Ehrlicher , setEnabled is not just a static function in my truth class, it is also a Qt function that isn't static and is part of the QWidget class which truth is derived from.

              Kind Regards,
              Sy

              KroMignonK 1 Reply Last reply
              0
              • SPlattenS SPlatten

                I have cleaned and rebuilt this project a couple of times just to see if this still comes up, I am getting:

                C4189: 'pTruth': local variable is initialised but not referenced
                

                The function this is occurring on is:

                void truth::setEnabled(bool blnEnable)
                {
                    if ( truth::mspSpinMax != nullptr )
                    {
                        truth::mspSpinMax->setEnabled(blnEnable);
                    }
                    if ( truth::mspSpinMin != nullptr )
                    {
                        truth::mspSpinMin->setEnabled(blnEnable);
                    }
                    if ( truth::mspSlider != nullptr )
                    {
                        truth::mspSlider->setEnabled(blnEnable);
                        truth::mspSlider->setVisible(blnEnable);
                    }
                    foreach( truth* pTruth, truth::mslstAllTruths )
                    {
                        pTruth->setEnabled(false);
                    }
                }
                

                The yellow warning icon appears on the foreach line, clearly pTruth is used in the loop, so why is this warning being displayed?

                setEnabled public static function, here is the prototype:

                static void setEnabled(bool blnEnable);
                

                I am using Qt 5.9.2 with MSVC2015 on Windows 10.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @SPlatten said in C4189, why?:

                foreach

                also, please use the standard range for loop instead of foreach/Q_FOREACH there are reasons, why its deprecated and I believe even removed from Qt6 :D


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                sierdzioS 1 Reply Last reply
                3
                • SPlattenS SPlatten

                  @KroMignon, @Christian-Ehrlicher , setEnabled is not just a static function in my truth class, it is also a Qt function that isn't static and is part of the QWidget class which truth is derived from.

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #7

                  @SPlatten said in C4189, why?:

                  setEnabled is not just a static function in my truth class, it is also a Qt function that isn't static and is part of the QWidget class which truth is derived from.

                  This is not what you told us in your first post. How should we know this?

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  SPlattenS 1 Reply Last reply
                  0
                  • KroMignonK KroMignon

                    @SPlatten said in C4189, why?:

                    setEnabled is not just a static function in my truth class, it is also a Qt function that isn't static and is part of the QWidget class which truth is derived from.

                    This is not what you told us in your first post. How should we know this?

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #8

                    @KroMignon , sorry, I should have done that.

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @SPlatten said in C4189, why?:

                      foreach

                      also, please use the standard range for loop instead of foreach/Q_FOREACH there are reasons, why its deprecated and I believe even removed from Qt6 :D

                      sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #9

                      @J-Hilk said in C4189, why?:

                      @SPlatten said in C4189, why?:

                      foreach

                      also, please use the standard range for loop instead of foreach/Q_FOREACH there are reasons, why its deprecated and I believe even removed from Qt6 :D

                      It has been un-deprecated and is still present in Qt6.

                      But your recommendation is still very valid :-) It is better to use ranged for, with qAsConst().

                      (Z(:^

                      J.HilkJ 1 Reply Last reply
                      3
                      • SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #10

                        I have another issue which having resolved the original problem by using for( : ), I've now found that although:

                        pTruth->setEnabled(blnEnable);
                        

                        Is intended to call the QWidget version of setEnabled, it is actually calling the static version even though the correct way to call the static version would be:

                        truth::setEnabled(blnEnable);
                        

                        Is there a way I can resolve this?

                        Kind Regards,
                        Sy

                        J.HilkJ C KroMignonK 3 Replies Last reply
                        0
                        • sierdzioS sierdzio

                          @J-Hilk said in C4189, why?:

                          @SPlatten said in C4189, why?:

                          foreach

                          also, please use the standard range for loop instead of foreach/Q_FOREACH there are reasons, why its deprecated and I believe even removed from Qt6 :D

                          It has been un-deprecated and is still present in Qt6.

                          But your recommendation is still very valid :-) It is better to use ranged for, with qAsConst().

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #11

                          @sierdzio said in C4189, why?:

                          It has been un-deprecated and is still present in Qt6.

                          oO ok, I should follow the mailing list more closely...

                          personally nqAsConst() and such have always bothered me,
                          since c++20 we can now give the for loop init arguments so something like this should be possible :D

                           QVector<int> v;
                              for( auto const cVector = v; auto entry : cVector){
                                  
                              }
                          

                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          0
                          • SPlattenS SPlatten

                            I have another issue which having resolved the original problem by using for( : ), I've now found that although:

                            pTruth->setEnabled(blnEnable);
                            

                            Is intended to call the QWidget version of setEnabled, it is actually calling the static version even though the correct way to call the static version would be:

                            truth::setEnabled(blnEnable);
                            

                            Is there a way I can resolve this?

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #12

                            @SPlatten said in C4189, why?:

                            Is there a way I can resolve this?

                            call explicitly the base class method:

                            pTruth->QWidget::setEnabled(blnEnable)
                            

                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            3
                            • SPlattenS SPlatten

                              I have another issue which having resolved the original problem by using for( : ), I've now found that although:

                              pTruth->setEnabled(blnEnable);
                              

                              Is intended to call the QWidget version of setEnabled, it is actually calling the static version even though the correct way to call the static version would be:

                              truth::setEnabled(blnEnable);
                              

                              Is there a way I can resolve this?

                              C Offline
                              C Offline
                              ChrisW67
                              wrote on last edited by
                              #13

                              @SPlatten said in C4189, why?:

                              Is there a way I can resolve this?

                              Do not create a static function with the same name as the inherited one.

                              However, I do not really understand why all of the UI elements (mspSpinMax etc.) and this function are static in the first place.

                              SPlattenS 1 Reply Last reply
                              0
                              • C ChrisW67

                                @SPlatten said in C4189, why?:

                                Is there a way I can resolve this?

                                Do not create a static function with the same name as the inherited one.

                                However, I do not really understand why all of the UI elements (mspSpinMax etc.) and this function are static in the first place.

                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #14

                                @ChrisW67 , for encapsulation purposes, the truth widgets has related controls.

                                Kind Regards,
                                Sy

                                C 1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  I have another issue which having resolved the original problem by using for( : ), I've now found that although:

                                  pTruth->setEnabled(blnEnable);
                                  

                                  Is intended to call the QWidget version of setEnabled, it is actually calling the static version even though the correct way to call the static version would be:

                                  truth::setEnabled(blnEnable);
                                  

                                  Is there a way I can resolve this?

                                  KroMignonK Offline
                                  KroMignonK Offline
                                  KroMignon
                                  wrote on last edited by KroMignon
                                  #15

                                  @SPlatten said in C4189, why?:

                                  have another issue which having resolved the original problem by using for( : ), I've now found that although:
                                  pTruth->setEnabled(blnEnable);

                                  Is intended to call the QWidget version of setEnabled, it is actually calling the static version even though the correct way to call the static version would be:
                                  truth::setEnabled(blnEnable);

                                  Is there a way I can resolve this?

                                  AFAIK, you should never have static and class methods with same signature in C++ classes, because the last one will hide the previous one.
                                  But again, I am not a C++ expert, maybe there is a way to do it which I don't know.

                                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                  SPlattenS 1 Reply Last reply
                                  1
                                  • KroMignonK KroMignon

                                    @SPlatten said in C4189, why?:

                                    have another issue which having resolved the original problem by using for( : ), I've now found that although:
                                    pTruth->setEnabled(blnEnable);

                                    Is intended to call the QWidget version of setEnabled, it is actually calling the static version even though the correct way to call the static version would be:
                                    truth::setEnabled(blnEnable);

                                    Is there a way I can resolve this?

                                    AFAIK, you should never have static and class methods with same signature in C++ classes, because the last one will hide the previous one.
                                    But again, I am not a C++ expert, maybe there is a way to do it which I don't know.

                                    SPlattenS Offline
                                    SPlattenS Offline
                                    SPlatten
                                    wrote on last edited by
                                    #16

                                    @KroMignon , @J-Hilk solution works!

                                    Kind Regards,
                                    Sy

                                    1 Reply Last reply
                                    0
                                    • SPlattenS SPlatten

                                      @ChrisW67 , for encapsulation purposes, the truth widgets has related controls.

                                      C Offline
                                      C Offline
                                      ChrisW67
                                      wrote on last edited by
                                      #17

                                      @SPlatten Which are all adequately (and usually) encapsulated as members of the particular truth QWidget object. As it is now, AFAICT, all truth widgets have the same related widgets.

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved