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. ASSERT failure in QVector<T>::operator[]: "index out of range" Error
Forum Updated to NodeBB v4.3 + New Features

ASSERT failure in QVector<T>::operator[]: "index out of range" Error

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 462 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.
  • M Offline
    M Offline
    mena_mes
    wrote on last edited by mena_mes
    #1

    Hi Everyone,

    I want to count numer of lines in a file.

        QFile file("D:/test.txt");
        int line_count=0;
        file.open(QIODevice::ReadOnly); //| QIODevice::Text)
        QVector<QString> line;
        QTextStream in(&file);
        while( !in.atEnd())
        {
            line[line_count]=in.readLine();
            line_count++;
        }
    

    and I get the error " ASSERT failure in QVector<T>::operator[]: "index out of range"

    I know I have to add the size here QVector<QString> line (15000) ;

    but I am not sure if the file contains more than 15000 for example.

    Can anyone help me please :)

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

      Hi
      Its the
      line[line_count]

      as line is
      QVector<QString> line;
      and it don't preallocate any entries beforehand.
      (ah, you are aware of this. missed that)

      so you want to do https://doc.qt.io/qt-5/qvector.html#append

      line.append( readLine() );

      1 Reply Last reply
      5

      • Login

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