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
Forum Updated to NodeBB v4.3 + New Features

'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.5k 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.
  • L Offline
    L Offline
    LuAria
    wrote on 4 Sept 2017, 09:09 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 4 Sept 2017, 09:17 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

      L 1 Reply Last reply 4 Sept 2017, 12:30
      4
      • P Offline
        P Offline
        Pradeep Kumar
        wrote on 4 Sept 2017, 10:23 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

        L 1 Reply Last reply 4 Sept 2017, 13:02
        2
        • S SGaist
          4 Sept 2017, 09:17

          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";
          }
          
          L Offline
          L Offline
          LuAria
          wrote on 4 Sept 2017, 12:30 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
          • P Pradeep Kumar
            4 Sept 2017, 10:23

            Hi,

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

            Thanks,

            L Offline
            L Offline
            LuAria
            wrote on 4 Sept 2017, 13:02 last edited by
            #5

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

            V 1 Reply Last reply 4 Sept 2017, 13:04
            0
            • L LuAria
              4 Sept 2017, 13:02

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

              V Offline
              V Offline
              VRonin
              wrote on 4 Sept 2017, 13:04 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

              L 1 Reply Last reply 4 Sept 2017, 15:15
              2
              • V VRonin
                4 Sept 2017, 13:04

                @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{};

                L Offline
                L Offline
                LuAria
                wrote on 4 Sept 2017, 15:15 last edited by
                #7

                @VRonin Thank you very much!

                P 1 Reply Last reply 4 Sept 2017, 17:18
                0
                • L LuAria
                  4 Sept 2017, 15:15

                  @VRonin Thank you very much!

                  P Offline
                  P Offline
                  Pablo J. Rogina
                  wrote on 4 Sept 2017, 17:18 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

                  2/8

                  4 Sept 2017, 09:17

                  topic:navigator.unread, 6
                  • Login

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