Qt6.2.1 QList index out of bound
-
This is the output:
16:33:31: Starting /home/.../work/projects/build-qlistqt6test-Desktop_Qt_6_2_1_GCC_64bit-Debug/qlistqt6test...
Item count:1
Item index:1
ASSERT failure in QList::remove: "index out of range", file /opt/Qt5.15.0/6.2.1/gcc_64/include/QtCore/qlist.h, line 741
16:33:31: /home/.../work/projects/build-qlistqt6test-Desktop_Qt_6_2_1_GCC_64bit-Debug/qlistqt6test crashed. -
@sbela said in Qt6.2.1 QList index out of bound:
Item index:1
Then you're indeed out of bounds.
Just not sure why indexOf returns 1 instead of 0 -
@sbela I have many problems with your code. Does this really build?
AFAIK, to use QList you need a default constructor. So I would change
explicit A(QString name) : m_name(name) { }
toexplicit A(const QString &name = QString()) : m_name(name) { }
.Second, I may be wrong, but for my understanding
if (auto index = a_list.indexOf(A("ONE")) > -1)
will create a local variable index which is destroyed after the test.
And the calculation is also wrong your have doneindex = a_list.indexOf(A("ONE")) > -1
, which should be(index = a_list.indexOf(A("ONE"))) > -1
I would change this to:
int index; if ((index = a_list.indexOf(A("ONE"))) > -1) { std::cout << "Item index:" << index << std::endl; a_list.removeAt(index); }
-
@sbela said in Qt6.2.1 QList index out of bound:
if (auto index = a_list.indexOf(A("ONE")) > -1)
Oh dear! And this is why
index
is being set to1
@jsulmHint: Operator precedence.... :)
P.S.
@KroMignon
Your post crossed with mine.Second, I may be wrong, but for my understanding
if (auto index = a_list.indexOf(A("ONE")) > -1)
will create a local variable index which is destroyed after the test.No. Says in scope for whole of
if
body. New (well not very new) C++ thingie :)which should be
(index = a_list.indexOf(A("ONE"))) > -1
Indeed! But you gave it away! ;-) I assume the whole line with the
auto
would actually read:if ((auto index = a_list.indexOf(A("ONE"))) > -1)
-
Ok! But this:
if ((auto index = a_list.indexOf(A("ONE"))) > -1)
does not compile for me because:
error: expected primary-expression before ‘auto’ 52 | if ((auto index = a_list.indexOf(A("ONE"))) > -1) | ^~~~
@JonB said in Qt6.2.1 QList index out of bound:
if ((auto index = a_list.indexOf(A("ONE"))) > -1)
-
@sbela
It was only a guess, because I would never useauto
, and not in the condition of anif
!Can it be:
if (auto (index = a_list.indexOf(A("ONE"))) > -1)
? I can't believe that's right!
In which case, you tell me how you can both use
auto
(or explicit type specifier) and get the operator precedence right here, because it's not clicking with me :) And I'm not sure how to Google for this....P.S.
Did I recently see yet another C++-ism which would allow this:if (auto index; (index = a_list.indexOf(A("ONE"))) > -1)
? See that
;
and multi-statement in the condition? I believe that's a "thing" now with C++, might require a recent version? @jsulm , @KroMignon ?But that won't work with
auto
. So presumably either of:if (int index; (index = a_list.indexOf(A("ONE"))) > -1) if (auto index = a_list.indexOf(A("ONE")); index > -1)
-
If you want to use c++17 you should learn the syntax...
if (auto index = a_list.indexOf("ONE"); index > -1)
-
This works! But this is not what I want to write... :)
I want that "index" to be local to that "if" - if that is possible?! :)@KroMignon said in Qt6.2.1 QList index out of bound:
int index;
if ((index = a_list.indexOf(A("ONE"))) > -1)
{
std::cout << "Item index:" << index << std::endl;
a_list.removeAt(index);
} -