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

How to use QVector with double

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.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.
  • C Offline
    C Offline
    CJha
    wrote on 6 Aug 2020, 12:45 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
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 6 Aug 2020, 13:14 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};
      
      C J 2 Replies Last reply 6 Aug 2020, 13:29
      4
      • C Chris Kawa
        6 Aug 2020, 13:14

        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};
        
        C Offline
        C Offline
        CJha
        wrote on 6 Aug 2020, 13:29 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
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 6 Aug 2020, 13:32 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++".

          C 1 Reply Last reply 6 Aug 2020, 13:38
          2
          • C Chris Kawa
            6 Aug 2020, 13:14

            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 Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 6 Aug 2020, 13:34 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.

            C 1 Reply Last reply 6 Aug 2020, 13:42
            1
            • C Chris Kawa
              6 Aug 2020, 13:32

              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++".

              C Offline
              C Offline
              CJha
              wrote on 6 Aug 2020, 13:38 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 J.Hilk
                6 Aug 2020, 13:34

                @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

                C Offline
                C Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 6 Aug 2020, 13:42 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 1 Reply Last reply 6 Aug 2020, 13:56
                2
                • C Chris Kawa
                  6 Aug 2020, 13:42

                  @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 Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 6 Aug 2020, 13:56 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

                  1/8

                  6 Aug 2020, 12:45

                  • Login

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