filling a Qvector using a pointer to it



  • 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


  • Qt Champions 2017

    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.



  • 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);
    


  • 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);
    }
    


  • 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];
                 }
         }
    }
    


  • @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.



  • @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);
    


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



  • Thanks, I'll try that.


  • Qt Champions 2017

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.