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. filling a Qvector using a pointer to it

filling a Qvector using a pointer to it

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 2.9k 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.
  • Z Offline
    Z Offline
    Zhitoune
    wrote on last edited by
    #1

    Dear All,

    i'm having a little problem using a QVector. So my program receives data periodicaly and I like to transfert the data to a vector that is bigger than the amount of data I receive.
    the Size of the vector is set in the setVectorSize() function and the is being filled in the transfertData(). So the vector has to be a global variable for the class. it's size can change along whith the user acquisition parameters.

    main.h

    QVector<int32>  *measures ;
    void setVectorSize();
    void transfertData(int *data, int dataSize);
    

    main.cpp

    void main::setVectorSize()
    {
          measures = newQVector<int32>(200);
          int I  = 0;
    }
    void main::transfertData(int *data, int dataSize)
    {
         //with dataSize = 10 
          for (int i = 0 ; i < dataSize ; i++)
         {
                measures[I] = data[i]; // does not compile (error below)
                I = I++;
         }
         end
    }
    

    Except that it doesn't compile, I get the following error :
    C2679: binary '=': no operator found which takes a right-hand operand of type 'int32' (or there is no acceptable conversion)

    I'm pretty sure it's a simple problem but I can't get may head around it. any help would be welcome.
    thanks in advace for your help

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

      Hi
      (*measures)[0] = 12;
      You must de-ref it.
      Do you need it as pointer ?
      why not just
      QVector<int32> measures ;
      You can always call clear t empty it etc so not sure why it has to be pointer to vector.
      Not that it is super important but you must then remember to delete it when main is destroyed.

      1 Reply Last reply
      3
      • 6thC6 Offline
        6thC6 Offline
        6thC
        wrote on last edited by
        #3

        Is this code complete, cause it looks like you acquire and instance an int but then discard the variable I in setVectorSize - but then try to use it it transfertData:

        measures[I] = data[i];
        

        also

        measures = newQVector<int32>(200);
        

        is missing a space, should it not be?

        measures = new QVector<int32>(200);
        
        1 Reply Last reply
        3
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          operators are methods so you are still free to call them explicitly: measures->operator[](i) = data[i]
          or leverage the stl

          void transfertData(int *data, int dataSize)
          {
          if(!data) return;
          Q_ASSERT(I+dataSize<=measures->size());
          std::copy(data,data+dataSize,measures->begin()+I);
          }
          

          "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

          1 Reply Last reply
          2
          • Z Offline
            Z Offline
            Zhitoune
            wrote on last edited by Zhitoune
            #5

            Thanks for your answers!

            @mrjj well I tried to declare it plainly

            void main::setVectorSize()
            {
               QVector<int32> measures(200);
                  int I  = 0;
            }
            

            but then in trasfertData() it's size would be zero causing the program to crash

            void main::tranfertData(int *data, int dataSize)
            {
                   measures.size(); // produce zero
            }
            

            @6thC well no this code is not complete, it's a partial copy of my code.

            the trick about this code is that it takes several call of transfertdata() to fill measures that's why I need to be able to set its size outside of the transfertData() and before the data acquisition starts.
            the real code of transfertData() is :

            void main::transfertData(int *data, int dataSize)
            {
                 //with dataSize = 10 
                  for (int i = 0 ; i < dataSize ; i++)
                 {
                        if (I<measures.size())
                        {
                                 measures[I] = data[i]; // does not compile (error below)
                                  I++;
                         }
                         else
                         {
                                 I = 0;
                                  measures[I] = data[i];
                         }
                 }
            }
            
            Taz742T 1 Reply Last reply
            0
            • Z Zhitoune

              Thanks for your answers!

              @mrjj well I tried to declare it plainly

              void main::setVectorSize()
              {
                 QVector<int32> measures(200);
                    int I  = 0;
              }
              

              but then in trasfertData() it's size would be zero causing the program to crash

              void main::tranfertData(int *data, int dataSize)
              {
                     measures.size(); // produce zero
              }
              

              @6thC well no this code is not complete, it's a partial copy of my code.

              the trick about this code is that it takes several call of transfertdata() to fill measures that's why I need to be able to set its size outside of the transfertData() and before the data acquisition starts.
              the real code of transfertData() is :

              void main::transfertData(int *data, int dataSize)
              {
                   //with dataSize = 10 
                    for (int i = 0 ; i < dataSize ; i++)
                   {
                          if (I<measures.size())
                          {
                                   measures[I] = data[i]; // does not compile (error below)
                                    I++;
                           }
                           else
                           {
                                   I = 0;
                                    measures[I] = data[i];
                           }
                   }
              }
              
              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on last edited by Taz742
              #6

              @Zhitoune said in filling a Qvector using a pointer to it:

              measures[I] = data[i]; // does not compile (error below)

              what error ?

              @Zhitoune said in filling a Qvector using a pointer to it:

              void main::transfertData(int *data, int dataSize)
              {
              //with dataSize = 10
              for (int i = 0 ; i < dataSize ; i++)
              {
              if (I<measures.size())
              {
              measures[I] = data[i]; // does not compile (error below)
              I = I++;
              }
              else
              {
              I = 0;
              measures[I] = data[i];
              }
              }
              }

              where is a declared 'I' ?

              @Zhitoune said in filling a Qvector using a pointer to it:

              void main::tranfertData(int *data, int dataSize)
              {
              measures.size(); // produce zero
              }

              because in your setVectorSize function:

              @Zhitoune said in filling a Qvector using a pointer to it:

              void main::setVectorSize()
              {
              QVector<int32> measures(200);
              int I = 0;
              }

              measures after a function is out of scope. it should be an member variable.

              Do what you want.

              Z 1 Reply Last reply
              2
              • Taz742T Taz742

                @Zhitoune said in filling a Qvector using a pointer to it:

                measures[I] = data[i]; // does not compile (error below)

                what error ?

                @Zhitoune said in filling a Qvector using a pointer to it:

                void main::transfertData(int *data, int dataSize)
                {
                //with dataSize = 10
                for (int i = 0 ; i < dataSize ; i++)
                {
                if (I<measures.size())
                {
                measures[I] = data[i]; // does not compile (error below)
                I = I++;
                }
                else
                {
                I = 0;
                measures[I] = data[i];
                }
                }
                }

                where is a declared 'I' ?

                @Zhitoune said in filling a Qvector using a pointer to it:

                void main::tranfertData(int *data, int dataSize)
                {
                measures.size(); // produce zero
                }

                because in your setVectorSize function:

                @Zhitoune said in filling a Qvector using a pointer to it:

                void main::setVectorSize()
                {
                QVector<int32> measures(200);
                int I = 0;
                }

                measures after a function is out of scope. it should be an member variable.

                Z Offline
                Z Offline
                Zhitoune
                wrote on last edited by
                #7

                @Taz742
                this error : C2679: binary '=': no operator found which takes a right-hand operand of type 'int32' (or there is no acceptable conversion)

                I is a member variable and measures as well :

                main.h

                int I;
                QVector<int32>  *measures ;
                void setVectorSize();
                void transfertData(int *data, int dataSize);
                
                kshegunovK 1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  then remove QVector<int32> measures(200); otherwise it will be shadowed. use measures.resize(200);

                  "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

                  1 Reply Last reply
                  4
                  • Z Offline
                    Z Offline
                    Zhitoune
                    wrote on last edited by
                    #9

                    Thanks, I'll try that.

                    1 Reply Last reply
                    0
                    • Z Zhitoune

                      @Taz742
                      this error : C2679: binary '=': no operator found which takes a right-hand operand of type 'int32' (or there is no acceptable conversion)

                      I is a member variable and measures as well :

                      main.h

                      int I;
                      QVector<int32>  *measures ;
                      void setVectorSize();
                      void transfertData(int *data, int dataSize);
                      
                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      Use either std::copy as @VRonin wrote or fall back to the plain C memcpy. There's no reason to skip on the possible optimizations the CRT/STL could provide.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      3

                      • Login

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