Help me code and understand "foreach"
-
Trying to iterate thru "localAdapters" using "foreach".
I do not fully understand the "foreach" parameters.localAdapters = QBluetoothLocalDevice::allDevices(); int index; text = localAdapters.at(index).address().toString(); // run foreach foreach(text,localAdapters) { qDebug()<< text; }
Of course my code is wrong ....
I got way too many errors to analyze ..../mnt/07b7c3f8-0efb-45ab-8df8-2a468771de1f/PROJECTS/BT_JAN16_/CCC_SOURCE/btchat/chat.cpp:222: error: no viable overloaded '=' chat.cpp:222:9: error: no viable overloaded '=' foreach(text,localAdapters) ^~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qglobal.h:1126:21: note: expanded from macro 'foreach' # define foreach Q_FOREACH ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qglobal.h:1118:19: note: expanded from macro 'Q_FOREACH' for (variable = *_container_.i; _container_.control; _container_.control = 0) ~~~~~~~~ ^ ~~~~~~~~~~~~~~ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:270:14: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'QChar' for 1st argument QString &operator=(QChar c); ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:271:14: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'const QString' for 1st argument QString &operator=(const QString &) noexcept; ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:272:14: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'QLatin1String' for 1st argument QString &operator=(QLatin1String latin1); ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:274:21: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'QString' for 1st argument inline QString &operator=(QString &&other) noexcept ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:841:40: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'const char *' for 1st argument inline QT_ASCII_CAST_WARN QString &operator=(const char *ch) ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:843:40: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'const QByteArray' for 1st argument inline QT_ASCII_CAST_WARN QString &operator=(const QByteArray &a) ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:845:40: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'char' for 1st argument inline QT_ASCII_CAST_WARN QString &operator=(char c) ^ /home/nov25-1/Qt/Nov26/5.15.2/gcc_64/include/QtCore/qstring.h:956:21: note: candidate function not viable: no known conversion from 'const QBluetoothHostInfo' to 'const QString::Null' for 1st argument inline QString &operator=(const Null &) { *this = QString(); return *this; } ^
-
See your question 23 days ago: https://forum.qt.io/topic/141587/how-to-implement-for-foreach-iteration and the documentation: https://doc.qt.io/qt-6/foreach-keyword.html
-
@AnneRanch said in Help me code and understand "foreach":
QBluetoothLocalDevice
Try
for ( auto const & host_info : QBluetoothLocalDevice::allDevices() ) { qDebug()<< host_info.address().toString(); }
-
Refer back to the source:
https://en.cppreference.com/w/cpp/language/range-for
Oh... "foreach" not "range based for" so the closest "standard" thing would be std::for_each, and as used above foreach is purely a Qt thing I believe.
-
Qts foreach is very old. Please consider using the new C++ language support for a very similar thing;
for (auto item : container) {
}You can find more information about that and it will likely be easier to understand when the compiler complains about problems.
-
Qts foreach is very old. Please consider using the new C++ language support for a very similar thing;
for (auto item : container) {
}You can find more information about that and it will likely be easier to understand when the compiler complains about problems.
@TomZ OK, I got the message... pretty much in same line as "new style connect"...
or it is not C++ "standard" ?
I really do not see why QT is inventing any of these macros anyway, obsolete or not. .I am little curious about the usage of type "auto".
Was it / is in not original "C" and I have seldom see it used.
Or am I mixing it up with "no type" is automatically "int"? -
@TomZ OK, I got the message... pretty much in same line as "new style connect"...
or it is not C++ "standard" ?
I really do not see why QT is inventing any of these macros anyway, obsolete or not. .I am little curious about the usage of type "auto".
Was it / is in not original "C" and I have seldom see it used.
Or am I mixing it up with "no type" is automatically "int"?@AnneRanch said in Help me code and understand "foreach":
I really do not see why QT is inventing any of these macros anyway, obsolete or not. .
They were invented to use new stuff despite having old compilers / no standard yet supported it but it's useful.
-
auto in c++ is automatic type determination based on the right side of the expression
it is handy as a shorthand for long convoluted iterator types, or things that are deeply embedded and where the return type is not obvious upon casual inspection.
auto x="my string"; // yields const char*
auto v=1; // yields int
auto b=56U; yields unsigned
auto i=std::map::begin(); // yields std::map::iteratorget it now?