How to use a QVector with a custom class



  • Hello everybody...i'm triying to use a QVector with a custom class, can please someone explain me that mecanism? i try this QVector<CoupleStride>.... but the compilation return an error looking like this "ASSERT failure in QVector<T>::operator[]: "index out of range"...


  • Qt Champions 2016

    Hi
    There is nothing special QVector needs
    and can be used anywhere in code.

    What it says is that you use a index that is too big.

    So you have error in your code.
    Some where you do
    mylist[INDEX] and index is not
    with in legal range.

    -compilation return an error

    It only happens when you RUN the app, right ?

    also:
    if you only do
    QVector<CoupleStride> mylist;

    The list is empty!
    you must append some CoupleStride to it.



  • It only happens when you RUN the app, right ?// yes
    In fact, i'm triying to return that QVector<CoupleStride> in a function.


  • Qt Champions 2016

    @kasysbi

    well the error is from crashing the list using [].
    Returning it should not crash it.

    can you show the crashing code ?
    You can single step with debugger to find it or
    look in the call stack after the crash.



  • this appear on the screen
    ASSERT failure in QVector<T>::operator[]: "index out of range", file..........
    ......\Qt\Qt5.2.1\5.2.1\mingw48_32\include/QtCore/qvector.h.line 369
    ...
    end with the code 3



  • @kasysbi hi.
    can you post your full code?


  • Qt Champions 2016

    as @Taz742 says, we need to see your code
    where you use the list.
    Also where you append to list and what size you except it to be.



  • @kasysbi
    Use this:

    #include "mainwindow.h"
    #include <QApplication>
    #include "QLocale"
    #include "QDebug"
    #include "QJsonArray"
    #include "QJsonDocument"
    #include "QJsonValue"
    #include "QJsonObject"
    #include "QTextCodec"
    
    class QtIsMyLife{
        public:
            int Age;
            QString Name;
            QString Familyname;
    
            QtIsMyLife(int _age, QString _name, QString _familyname) {
                this->Age = _age;
                this->Name = _name;
                this->Familyname = _familyname;
            }
    };
    
    QVector<QtIsMyLife*> GetNewVector() {
        QVector<QtIsMyLife*> my_vector;
        my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        my_vector.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
    
        return my_vector;
    }
    
    void GetNewVector(QVector<QtIsMyLife*> &vec) {
        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
        vec.push_back(new QtIsMyLife(21, "Tazo", "Leladze"));
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        QVector<QtIsMyLife*> vec = GetNewVector();
    
        qDebug() << vec.size();
    
        QVector<QtIsMyLife*> vec_2;
    
        GetNewVector(vec_2);
    
        qDebug() << vec_2.size();
    
        return a.exec();
    }
    


  • @Taz742
    that was very helpfull thanks a lot...and it seems like use "data" pointer is better than use the [] operator with the main vector...thanks!


Log in to reply
 

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