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. How to use QVector with double
Qt 6.11 is out! See what's new in the release blog

How to use QVector with double

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by
    #1

    Hi! It might seem like a silly question but I am unable to initialize QVector. Here is what is happening:

    QVector<double> vec(10); // Error: Expected a type specifier
    

    I am initializing this in my .h file and I am using #include <QVector>. Isn't this how we are supposed to initialize QVector?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Isn't this how we are supposed to initialize QVector?

      Yes and no. This is how you'd initialize a local variable in a function.
      If it's a class member you can initialize it like this

      QVector<double> vec = QVector<double>(10);
      

      or with braces if it's not too much to type:

      QVector<double> vec {0,0,0,0,0,0,0,0,0,0};
      
      CJhaC J.HilkJ 2 Replies Last reply
      4
      • Chris KawaC Chris Kawa

        Isn't this how we are supposed to initialize QVector?

        Yes and no. This is how you'd initialize a local variable in a function.
        If it's a class member you can initialize it like this

        QVector<double> vec = QVector<double>(10);
        

        or with braces if it's not too much to type:

        QVector<double> vec {0,0,0,0,0,0,0,0,0,0};
        
        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on last edited by
        #3

        @Chris-Kawa Thank you! I knew it had to be something simple like this but was unable to find it on the internet.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          To be honest initialization in C++ is a bit of a mess and it's easy to get it wrong.
          There's a nice talk about this and related stuff from few years back: "The Nightmare of Initialization in C++".

          CJhaC 1 Reply Last reply
          2
          • Chris KawaC Chris Kawa

            Isn't this how we are supposed to initialize QVector?

            Yes and no. This is how you'd initialize a local variable in a function.
            If it's a class member you can initialize it like this

            QVector<double> vec = QVector<double>(10);
            

            or with braces if it's not too much to type:

            QVector<double> vec {0,0,0,0,0,0,0,0,0,0};
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @Chris-Kawa said in How to use QVector with double:

            or with braces if it's not too much to type:

            QVector<double> vec {0,0,0,0,0,0,0,0,0,0};
            

            or

            QVector<double> vd(10,0.0);

            if its too much to type , but the same value


            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.

            Chris KawaC 1 Reply Last reply
            1
            • Chris KawaC Chris Kawa

              To be honest initialization in C++ is a bit of a mess and it's easy to get it wrong.
              There's a nice talk about this and related stuff from few years back: "The Nightmare of Initialization in C++".

              CJhaC Offline
              CJhaC Offline
              CJha
              wrote on last edited by
              #6

              @Chris-Kawa Yeah, sometimes it is very frustrating with simple things, especially since I am new to C++. Tanks for the video link, I will watch it soon in my spare time :)

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

                @Chris-Kawa said in How to use QVector with double:

                or with braces if it's not too much to type:

                QVector<double> vec {0,0,0,0,0,0,0,0,0,0};
                

                or

                QVector<double> vd(10,0.0);

                if its too much to type , but the same value

                Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @J-Hilk said:

                QVector<double> vd(10,0.0);
                if its too much to type , but the same value

                That's not gonna compile as a class member declaration. As a local variable it's the same as QVector<double> vd(10) as 0.0 is the default value for double.

                J.HilkJ 1 Reply Last reply
                2
                • Chris KawaC Chris Kawa

                  @J-Hilk said:

                  QVector<double> vd(10,0.0);
                  if its too much to type , but the same value

                  That's not gonna compile as a class member declaration. As a local variable it's the same as QVector<double> vd(10) as 0.0 is the default value for double.

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

                  @Chris-Kawa

                  fair enough 😉

                  but,
                  ...
                  as member initialization, you could do:

                  QVector<double> vec2{QVector<double>(10)};
                  QVector<double> vec{QVector<double>(10,0.0)};
                  

                  you're right, there are way to many ways to initialize stuff in c++ !


                  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
                  2

                  • Login

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