Results 1 to 6 of 6

Thread: Runtime QVector "index out of range" error. How to fix it?

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Runtime QVector "index out of range" error. How to fix it?

    Good evening!

    What is wrong with the following code? I'm trying to use "insertPattern" method to return a QVector<Number>("Number" is an object i created) to "patternList". Then, push_back it into a QList. I'm not getting any compilation error message, but when i run it i get that error!

    Qt Code:
    1. QList<QVector<Number>> RegNumber::patternList(QList<QVector<Number>> pl, int array[], int sizeArray)
    2. // Irrelevant code here.
    3. for(...) {//If i delete this "for" and call "insertPattern" one time, the program runs fine.
    4. // Irrelevant code here.
    5. pl.push_back( insertPattern(array, sizeArray) );
    6. }
    7.  
    8.  
    9. QVector<Number> RegNumber::insertPattern(int array[], int sizeArray)
    10. {
    11. QVector<Number> pattern;
    12. pattern.push_back( Number(0, 1) );
    13.  
    14. for(int i = 0; i < sizeArray; i++)
    15. {
    16. if(array[i] != 0)
    17. pattern.push_back( Number((i + 2), array[i]) ) ;
    18. }
    19.  
    20. return pattern;
    21. }
    To copy to clipboard, switch view to plain text mode 

    ASSERT failure in QVector(T):perator[]: "index out of range", file C:\Qt\5.4\mingw491_32\include\QtCore/qvector.h, line 396
    How can i fix it?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Runtime QVector "index out of range" error. How to fix it?

    Is this your actual code? What you posted wouldn't cause such error. You are not using the indexing operator on a vector here anywhere. Post your real code, please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Runtime QVector "index out of range" error. How to fix it?

    Actually i'm using index operator, but in QList, not in QVector, that's why i didn't post my entire code. If i try to insert only one QVector in that QList the program runs fine, but if i try to enter with other QVectors i get that error message.

    The following code is the entire "patternList" method, the other one, "insertPattern" i posted in my first post.
    Qt Code:
    1. QList<QVector<Number>> RegNumber::patternList(QList<QVector<Number>> pl, int array[], int sizeArray) {
    2. int sizeLst = pl.size( );
    3. int sizeVec, j;
    4. bool equal = true;
    5.  
    6. if( sizeLst ) {
    7. for(int i = 0; i < sizeLst; i++) {
    8.  
    9. sizeVec = pl[i].size( );
    10. if((sizePatternFound(array, sizeArray)) == (sizeVec - 1)) {
    11.  
    12. equal = true;
    13. for(j = 1; j < sizeArray; j++) {
    14.  
    15. if((j + 1) != pl[i][j].val)
    16. equal = false;
    17. else if(array[j - 1] != pl[i][j].acum)
    18. equal = false;
    19. }
    20.  
    21. if( equal ) {
    22. pl[i][0].acum += 1;
    23. i = sizeLst;
    24. }
    25. }
    26. else {
    27. equal = false;
    28. }
    29. }
    30.  
    31. if( !equal ) {
    32. pl.push_back( insertPattern(array, sizeArray) );
    33. }
    34. }
    35. else {
    36. pl.push_back( insertPattern(array, sizeArray) );
    37. }
    38.  
    39. return pl;
    40. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Runtime QVector "index out of range" error. How to fix it?

    You are using vector's indexing operator three times -- in lines #15, #17 and #22.The last one can assert if the vector is empty, the previous two when sizeArray has wrong value (off by one?).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Runtime QVector "index out of range" error. How to fix it?

    So, try to imagine a list of vectors(arrays with different size of Number) in which the first node "pl[i][0].acum" of each is used to calculate(increment) how many times that combination of Number(pl[i][1...n].val and acum) appears in other place of my program that doesn't show any arror. Tha't why i used "pl[i][0].acum += 1" in line number 22, because that pattern of Number is in the list so i don't need to insert another one, just increment.

    I regret of one thing actually, instead of using array and be forced to do that weird thing in lines 15 and 17 i should use QVector of Number. The code would be much much more legible. Mistakes you see only after doing!

    Doing this way i just have to check if two QVectors(the in the position x of the list and the one passed as arg) are equal, if yes, increments, if not insert him in the list.
    Last edited by robgeek; 13th May 2015 at 16:48.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Runtime QVector "index out of range" error. How to fix it?

    I'm only pointing out places which might generate the error. You can place asserts before using the operators that check validity of the call.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 2nd March 2015, 16:27
  2. Replies: 0
    Last Post: 9th January 2015, 11:09
  3. Replies: 0
    Last Post: 11th April 2013, 13:29
  4. Handling "index out of range"
    By liqxpil in forum Qt Programming
    Replies: 1
    Last Post: 29th December 2010, 00:25
  5. setWritingSystem gives "out of range" error
    By jano_alex_es in forum Newbie
    Replies: 0
    Last Post: 2nd October 2009, 12:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.