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. 'a' may be used uninitialized in this function
QtWS25 Last Chance

'a' may be used uninitialized in this function

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.9.1windows10mingw32bitmysql 32bit
8 Posts 5 Posters 40.4k Views
  • 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.
  • LuAriaL Offline
    LuAriaL Offline
    LuAria
    wrote on last edited by
    #1

    I define a variable: int a.
    Then I assigned a=query.value(0).toInt(). (I used qt to connect to mysql)
    Then I used a in if().
    But when I built the project it warned me "'a' may be used uninitialized in this function"
    What happened to my project.
    Platform: Windows 10.
    Qt: 5.9.1
    mysql: 5.7.18

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      As silly as it may sound: exactly what the compiler told you.

      Without the full code, here's my educated guess:

      somewhere in your function:
      int a;
      
      somewhere else in the same function:
      if (query.next()) {
          a = query.value(0).toInt()
      }
      
      if (a == 42) { //<< if query.next() returned false, a is still uninitialised hence the warning.
          qDebug() << "The life, the universe and the rest";
      }
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      LuAriaL 1 Reply Last reply
      4
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by
        #3

        Hi,

        When u declare the variable make sure u initialize the variables, to avoid warnings.

        Thanks,

        Pradeep Kumar
        Qt,QML Developer

        LuAriaL 1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

          As silly as it may sound: exactly what the compiler told you.

          Without the full code, here's my educated guess:

          somewhere in your function:
          int a;
          
          somewhere else in the same function:
          if (query.next()) {
              a = query.value(0).toInt()
          }
          
          if (a == 42) { //<< if query.next() returned false, a is still uninitialised hence the warning.
              qDebug() << "The life, the universe and the rest";
          }
          
          LuAriaL Offline
          LuAriaL Offline
          LuAria
          wrote on last edited by
          #4

          @SGaist Yes, it like what you write.
          The warning is :
          C:\Users\Administrator\Desktop\TS\TS\buyticket.cpp:82: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
          if(a<1){

          1 Reply Last reply
          0
          • Pradeep KumarP Pradeep Kumar

            Hi,

            When u declare the variable make sure u initialize the variables, to avoid warnings.

            Thanks,

            LuAriaL Offline
            LuAriaL Offline
            LuAria
            wrote on last edited by
            #5

            @Pradeep-Kumar How to initialize the variables?
            Maybe I don't down that code, sorry.

            VRoninV 1 Reply Last reply
            0
            • LuAriaL LuAria

              @Pradeep-Kumar How to initialize the variables?
              Maybe I don't down that code, sorry.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @LuAria said in 'a' may be used uninitialized in this function:

              How to initialize the variables?

              instead of int a; use int a=0; or int a{};

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              LuAriaL 1 Reply Last reply
              2
              • VRoninV VRonin

                @LuAria said in 'a' may be used uninitialized in this function:

                How to initialize the variables?

                instead of int a; use int a=0; or int a{};

                LuAriaL Offline
                LuAriaL Offline
                LuAria
                wrote on last edited by
                #7

                @VRonin Thank you very much!

                Pablo J. RoginaP 1 Reply Last reply
                0
                • LuAriaL LuAria

                  @VRonin Thank you very much!

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #8

                  @LuAria please don't forget to mark your post as solved when your problem is finally gone :-)

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  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