QList takeLast() and removeLast() conflict
-
When I try to run this code
@
QStringList foo;
foo << "bar";
QString last;
last = foo.takeLast();
qDebug() << last;
foo.removeLast();
@Qt Creator raises this error:
ASSERT: "!isEmpty()" in file ......\Qt\Qt5.3.0\5.3\mingw482_32\include/QtCore/qlist.h, line 299
If I disable the line with removeLast() command by toggling comment the code works fine.
I do not understand this behavior since at takeLast() call the QStringList has not yet been emptied.
Is a bug? is there a way to save the last before QStringList is emptied? -
Hi,
[quote]I do not understand this behavior since at takeLast() call the QStringList has not yet been emptied.[/quote]It has been emptied. takeLast() removes the last item in your list.
Your program crashed because you called removeLast() on an empty list.
The details are in the documentation: http://qt-project.org/doc/qt-5/QList.html (QStringList is a typedef of QList<QString>)
-
[quote author="Imhotep" date="1405937374"]removeLast() is called before takeLast(). The list is not yet empty at the moment of takeLast() call[/quote]Your original code called takeLast() before removeLast().
But anyway, if your list contains one item, then...
- ...calling removeLast() will make your list empty.
- ...calling takeLast() will make your list empty.