How to use a QVector with a custom class
Solved
General and Desktop
-
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"...
-
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. -
-
@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(); }