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. Getting error when trying to access 2D QVector's elements.

Getting error when trying to access 2D QVector's elements.

Scheduled Pinned Locked Moved General and Desktop
errorqvector
3 Posts 2 Posters 1.3k 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.
  • B Offline
    B Offline
    borasemiz
    wrote on last edited by
    #1

    Hello, I would like to create a matrix by using QVector. Could you tell me where I make mistake? Here is the code:

    #include <QCoreApplication>
    #include <QVector>
    #include <QDebug>
    
    int main(int argc, char* argv[]) {
        QCoreApplication a(argc, argv);
        QVector< QVector<double> > list(3);
    
        QVector<double> first(5, 1.0);
        QVector<double> second(5, 2.0);
        QVector<double> third(5, 3.0);
    
        list.append(first);
        list.append(second);
        list.append(third);
    
        qDebug() << list.at(0).at(0);
    
        return a.exec();
    }
    

    It is compiled successfully, but when I run it, it prompts a console output which says:

    ASSERT failure in QVector<T>::at: "index out of range", file ../../Qt/5.4/gcc_64/include/QtCore/qvector.h, line 388
    

    How can I solve this? If I use standard library vector class, would it solve?

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

      Hi,

      You create list with three empty elements and then you append your three other vectors. So you have in fact list size that is 6 and the first three are empty hence the assert failure.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        You create list with three empty elements and then you append your three other vectors. So you have in fact list size that is 6 and the first three are empty hence the assert failure.

        B Offline
        B Offline
        borasemiz
        wrote on last edited by borasemiz
        #3

        @SGaist Thank you :) This solved my problem.

        1 Reply Last reply
        0

        • Login

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