QList with Struct issue in QT6
-
I would like to use a QList of a struct, and in particular to use the "contains" function.
I have a nasty issue, that can be replicated using the following code:struct STest{ int i1,i2; bool operator== (const STest & x); }; QList <STest> myList; STest test1={1,2}, test2={11,12}; myList.append(test1); myList.append(test2); bool contains; contains=myList.contains(test1); qDebug()<<contains;
It compiles without warnings and runs smoothly in Qt 5.15, but cannot compile in Qt6.4. I get the error message shown at the end of this post.
Despite of the many attempts I made I couls not sort this out.
Any suggestions? Thank you in advance.D:\OneDrive - University of Pisa\Documenti\C++\Qt\untitled\mainwindow.cpp:28: error: passing 'const STest' as 'this' argument discards qualifiers [-fpermissive] In file included from C:/Qt/6.4.0/mingw_64/include/QtCore/qobject.h:13, from C:/Qt/6.4.0/mingw_64/include/QtWidgets/qwidget.h:9, from C:/Qt/6.4.0/mingw_64/include/QtWidgets/qmainwindow.h:8, from C:/Qt/6.4.0/mingw_64/include/QtWidgets/QMainWindow:1, from ..\untitled\mainwindow.h:4, from ..\untitled\mainwindow.cpp:1: C:/Qt/6.4.0/mingw_64/include/QtCore/qlist.h: In instantiation of 'qsizetype QtPrivate::indexOf(const QList<T>&, const U&, qsizetype) [with V = STest; U = STest; qsizetype = long long int]': C:/Qt/6.4.0/mingw_64/include/QtCore/qlist.h:932:30: required from 'qsizetype QListSpecialMethodsBase<T>::indexOf(const AT&, qsizetype) const [with AT = STest; T = STest; qsizetype = long long int]' C:/Qt/6.4.0/mingw_64/include/QtCore/qlist.h:44:31: required from 'bool QListSpecialMethodsBase<T>::contains(const AT&) const [with AT = STest; T = STest]' ..\untitled\mainwindow.cpp:28:29: required from here C:/Qt/6.4.0/mingw_64/include/QtCore/qlist.h:903:20: error: passing 'const STest' as 'this' argument discards qualifiers [-fpermissive] 903 | if (*n == u) | ~~~^~~~ ..\untitled\mainwindow.cpp:10:6: note: in call to 'bool STest::operator==(const STest&)' 10 | bool STest::operator== (const STest &x){ | ^~~~~
-
The compiler also tells you that you want to use a non-const function but expects a const one:
passing 'const STest' as 'this' argument discards qualifiers [-fpermissive]
-
The compiler also tells you that you want to use a non-const function but expects a const one:
passing 'const STest' as 'this' argument discards qualifiers [-fpermissive]
@Christian-Ehrlicher Yes, the issue is somehow reated to the "constantness" of something.
But I don't understand how to solve this. To be acceptable by 5.15 the code requires 'const' in the parenthesis of the operator ==.I even suspect that my example is correct since Qt 5.15 does not give any warnings or errors. In that case I would consider accceptable to bypass the Qt6 error message, but I don't know how to do this.
Maybe setting "-fpermissive"? But how to do this? -
@Christian-Ehrlicher Yes, the issue is somehow reated to the "constantness" of something.
But I don't understand how to solve this. To be acceptable by 5.15 the code requires 'const' in the parenthesis of the operator ==.I even suspect that my example is correct since Qt 5.15 does not give any warnings or errors. In that case I would consider accceptable to bypass the Qt6 error message, but I don't know how to do this.
Maybe setting "-fpermissive"? But how to do this?@Ceraolo_gmail
You are supposed to change your==
operator declarationto match exactly the one in the docs,so:bool operator== (const STest & x) const;
It is the
const
at the end that you are missing. -
@Christian-Ehrlicher Yes, the issue is somehow reated to the "constantness" of something.
But I don't understand how to solve this. To be acceptable by 5.15 the code requires 'const' in the parenthesis of the operator ==.I even suspect that my example is correct since Qt 5.15 does not give any warnings or errors. In that case I would consider accceptable to bypass the Qt6 error message, but I don't know how to do this.
Maybe setting "-fpermissive"? But how to do this?This post is deleted! -
@Ceraolo_gmail
You are supposed to change your==
operator declarationto match exactly the one in the docs,so:bool operator== (const STest & x) const;
It is the
const
at the end that you are missing.@JonB
Great! I could not find the piece of docs you linked.
This solves my issue.
Thanks a lot! -
You're right - the Qt documentation is not that clear about the constness but c++ is: https://en.cppreference.com/w/cpp/language/operator_comparison