Click here to Skip to main content
15,617,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am still not comfortable with using the NEW C++ " FOR " syntax.
My test code is to retrieve "name". I just want the "for" loop to function, I will add more retrieval info later.

I should not need the "index". Added for test purposes.
It did not work without index anyway.

Here is my latest attempt and the error .
I do not understand the error.

<pre>    QBluetoothLocalDevice localDevices;
 //   for(auto &localDeviceName:localDevice)
    int index;
    for(auto &localDevices.allDevices(index):localDevices)
        text = " TEST ";

    QString localDeviceName = localDevices.allDevices().at(0).name();

..and the error

<pre>/mnt/07b7c3f8-0efb-45ab-8df8-2a468771de1f/PROJECTS/JAN21/BT_JAN24_BT_SPP_/CCC_SOURCE/Bluetoooth_SPP_Connect/bt_spp_connect_mainwindow.cpp:29: error: declaration of variable 'localDevices' with deduced type 'auto &' requires an initializer
bt_spp_connect_mainwindow.cpp:29:15: error: declaration of variable 'localDevices' with deduced type 'auto &' requires an initializer
    for(auto &localDevices.allDevices(index):localDevices)

What I have tried:

QString localDeviceName

Added "normal" retrieval of "name"

QString localDeviceName = localDevices.allDevices().at(0).name();
Updated 24-Jan-23 11:05am

C++ ranged for loops look like
for( <variable decl> : <container> )

Where <variable decl=""> is a normal variable declaration. So for example
std:vector  v<int>:
// things get added to vector here

for(auto i : v) {
   // i will have type int here

so probably what you want is
for( auto& deviceName : localDevices ) {
    // work with device name 

This assumes that whatever type localDevices is it has the required member functions so that the ranged for loop can be executed, and that the type of an individual item can be deduced.
Share this answer
Member 14968771 24-Jan-23 16:04pm    
SOrry, but I still get same error.
OK , I underrated the "container"m but how can I "convert" this


to correct variable ?
k5054 24-Jan-23 16:32pm    
It looks like localDevices.allDevices() returns a container of some sort. So, correcting myself, maybe you want
auto allDevices = localDevices.allDevices(); // gets a list of devices
for( auto& device : allDevices ) {
    // device has the same type as the contained object of allDevices
    auto& name =;
    std::cout << name << '\n';
If you don't need to modify the device object within the for loop, then you should declare device as const auto& device
CPallini 24-Jan-23 16:28pm    
The range for loop needs a range-expression (e.g. a container).
The localDevices object is an instance of the BluetoothLocalDevice class. The latter is NOT a container, however it does provide the allDevices method, returning a list.
Hence, the proper way to write a range for loop in your scenario is:
for (auto & device : localDevices.allDevices() )
  // access device fields here, e.g.
Share this answer
k5054 24-Jan-23 17:21pm    
My 5. Your solution was more correct than mine.
Member 14968771 24-Jan-23 21:56pm    
I do not know " who is on first" , but I think YOU wrote that solution.
I am sorry , this has been answered elsewhere.

But this discussion has been very helpful too.

So the problem was how the "container" was defined...

auto localAdapters = QBluetoothLocalDevice::allDevices();
 qDebug()<< " localAdapters  count " << QString::number(localAdapters.count());
 for (const auto & device : localAdapters)
   qDebug()<< " local adapter address" << device.address().toString();
   //ui->chat_10->append( device.address().toString());
   qDebug() << " local adapater name " <<;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900