How to pass and simply iterate...C/C++
-
@Chris-Kawa Ok, as I thought - I need to pass the array index as a separate parameter so the iteration will go thru correct array. .
The iteration is limited to size of the array, so it is not in danger to "run over".
.@AnneRanch If you know that
ProcessCommand()
is just called with a single QStringList, it is better to have just a single QStringList as parameter and provide the correct QStringList from the array when calling this function. Then you don't have to pass the index to the function (ProcessCommand does not have any stakes in knowing it is processing an entry from an array).This would look like this:
// function declaration: QString BT_Utility_Library::ProcessCommand(char *command, const QStringList &verify); // calling the function BT_Utility_Library btul; char *command = ...; QStringList verify[MAX_LIST] = ...; ... btul.ProcessCommand(command, verify[0]);
Note that I again defined the QStringList parameter as reference. The difference to Java is that in Java everything is already a reference. This is why you don't have to specify it in Java. Almost always you are correct in using a const reference when passing objects. If you don't do this you will have copies all over the place which eventually slows down your application significantly.
-
One more question .
I am trying to have two dimensional array and cannot get the
for ( ....) to retrieve the members - it retrieves only the first sub array.// verify two dimensional list
for (auto &verify_list : *verify[index]) {
text = "verify list ";
text += verify_list;
qDebug() << text;
}then I get this error
/mnt/RAID_124/PROJECTS_MAR6_BAD_RECOVERY/CCC_SOURCE/BT_Utility_Library/bt_utility_library.cpp:95: error: indirection requires pointer operand ('QStringList' invalid)
bt_utility_library.cpp:95:40: error: indirection requires pointer operand ('QStringList' invalid)
for (auto &verify_list : *verify[index]) {
^~~~~~~~~~~~~~I do not know know how to implement "pointer operand" .
Help would be appreciated. -
One more question .
I am trying to have two dimensional array and cannot get the
for ( ....) to retrieve the members - it retrieves only the first sub array.// verify two dimensional list
for (auto &verify_list : *verify[index]) {
text = "verify list ";
text += verify_list;
qDebug() << text;
}then I get this error
/mnt/RAID_124/PROJECTS_MAR6_BAD_RECOVERY/CCC_SOURCE/BT_Utility_Library/bt_utility_library.cpp:95: error: indirection requires pointer operand ('QStringList' invalid)
bt_utility_library.cpp:95:40: error: indirection requires pointer operand ('QStringList' invalid)
for (auto &verify_list : *verify[index]) {
^~~~~~~~~~~~~~I do not know know how to implement "pointer operand" .
Help would be appreciated.@AnneRanch
indirection requires pointer operand
This means
*
(indirection operator) can be used on pointers and what you have here is QStringList, so not a pointer. You don't need*
here.it retrieves only the first sub array
See my previous response where I posted how to iterate over both dimensions.
for (auto &verify_list : *verify[index]) {
text = "verify list ";
text += verify_list;
qDebug() << text;
}Note that you're overwriting text in each loop iteration. If you want to join the strings from the list and display it as one string you would do it like this:
QString joined = verify[index].join(' '); qDebug() << joined;
If you want to display each string separately do it like this:
for (const auto& str : verify[index]) { text = "verify list " + str; qDebug() << text; }
-
@Chris-Kawa said in How to pass and simply iterate...C/C++:
for (const auto& verify_list : verify[0])
Here is my problem
'debug (verify) clearly reads correct array - index[1] , but the iterator (verify_list)
"prints" the wrong one - index [0] .What am I doing wrong?
-
@Chris-Kawa said in How to pass and simply iterate...C/C++:
for (const auto& verify_list : verify[0])
Here is my problem
'debug (verify) clearly reads correct array - index[1] , but the iterator (verify_list)
"prints" the wrong one - index [0] .What am I doing wrong?
@AnneRanch I see nothing wrong. From what I can tell from your picture the content of your structure is:
verify[0]
isQStringList { "Discovery started", "SPP_CA" }
verify[1]
isQStringList { "Controller", "Agent registered" }
The debugger shows
index
is 1 so the output showsverify list Controller verify list Agent registered
as expected. Are you maybe confused about what
*verify
is in the debug window? Since it's an array*verify
is the same asverify[0]
, so it's all as expected. -
@AnneRanch I see nothing wrong. From what I can tell from your picture the content of your structure is:
verify[0]
isQStringList { "Discovery started", "SPP_CA" }
verify[1]
isQStringList { "Controller", "Agent registered" }
The debugger shows
index
is 1 so the output showsverify list Controller verify list Agent registered
as expected. Are you maybe confused about what
*verify
is in the debug window? Since it's an array*verify
is the same asverify[0]
, so it's all as expected.Here is how the array looks and under index 1 the iterator should return
"Discovery started ' and "SPP_CA" - it does not. iT returns index 0.
. -
Here is how the array looks and under index 1 the iterator should return
"Discovery started ' and "SPP_CA" - it does not. iT returns index 0.
.@AnneRanch But that's not the same thing as what you have on the previous screenshot, so either the contents of the array are changing each time or I don't know what you're posting, because those two posts don't match.
And what iterator are you talking about? There are no iterators in that for loop you posted previously.You do a lot of copy pasting. Maybe you have two different variables with the same name in different places or something?
-
@AnneRanch But that's not the same thing as what you have on the previous screenshot, so either the contents of the array are changing each time or I don't know what you're posting, because those two posts don't match.
And what iterator are you talking about? There are no iterators in that for loop you posted previously.You do a lot of copy pasting. Maybe you have two different variables with the same name in different places or something?
@Chris-Kawa I am making some very basic mistake defining the array, but I just cannot see it.
I have added plain for(...) loop and forced the verify index to 0 and 1
The plain loop results does not make sense - the results are "reversed " .
The key . in my opinion, is in my definition of the two dimensional array- somewhere I am making a mistake ...
//#ifdef BYPASS // verify two dimensional list' for (const auto& verify_list : verify[index]) { //for (const auto &verify_list : *verify[index]) { text = "verify list "; text += verify_list; qDebug() << text; } int size = 2; for(int Loop_index =0; Loop_index != size ; Loop_index++) { text = verify[index][Loop_index]; qDebug() << text; } text = "Process index...zero "; text += QString::number(index); // _FUNC_INFO; qDebug() << text; for(int Loop_index =0; Loop_index != size ; Loop_index++) { text = verify[0][Loop_index]; qDebug() << text; } text = "Process index...one "; text += QString::number(index); // _FUNC_INFO; qDebug() << text; for(int Loop_index =0; Loop_index != size ; Loop_index++) { text = verify[1][Loop_index]; qDebug() << text; } return result;
"Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)" "Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)" "Process index...1" "verify list Controller" "verify list Agent registered" "Controller" "Agent registered" "Process index...zero 1" "Discovery started" "SPP_CA" "Process index...one 1" "Controller" "Agent registered" "void MainWindow_Bluetooth::Process_Menu_Index(int)"
#define MAX_ARRAY_VERIFY 16 QStringList verify[MAX_ARRAY_VERIFY] = { { // verify Agent registered "Controller", "Agent registered" }, { // verify Agent registered // temporqry MN "Discovery started", "SPP_CA" }, { // verify Agent registered "Controller", "Agent registered" } };
-
@Chris-Kawa I am making some very basic mistake defining the array, but I just cannot see it.
I have added plain for(...) loop and forced the verify index to 0 and 1
The plain loop results does not make sense - the results are "reversed " .
The key . in my opinion, is in my definition of the two dimensional array- somewhere I am making a mistake ...
//#ifdef BYPASS // verify two dimensional list' for (const auto& verify_list : verify[index]) { //for (const auto &verify_list : *verify[index]) { text = "verify list "; text += verify_list; qDebug() << text; } int size = 2; for(int Loop_index =0; Loop_index != size ; Loop_index++) { text = verify[index][Loop_index]; qDebug() << text; } text = "Process index...zero "; text += QString::number(index); // _FUNC_INFO; qDebug() << text; for(int Loop_index =0; Loop_index != size ; Loop_index++) { text = verify[0][Loop_index]; qDebug() << text; } text = "Process index...one "; text += QString::number(index); // _FUNC_INFO; qDebug() << text; for(int Loop_index =0; Loop_index != size ; Loop_index++) { text = verify[1][Loop_index]; qDebug() << text; } return result;
"Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)" "Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)" "Process index...1" "verify list Controller" "verify list Agent registered" "Controller" "Agent registered" "Process index...zero 1" "Discovery started" "SPP_CA" "Process index...one 1" "Controller" "Agent registered" "void MainWindow_Bluetooth::Process_Menu_Index(int)"
#define MAX_ARRAY_VERIFY 16 QStringList verify[MAX_ARRAY_VERIFY] = { { // verify Agent registered "Controller", "Agent registered" }, { // verify Agent registered // temporqry MN "Discovery started", "SPP_CA" }, { // verify Agent registered "Controller", "Agent registered" } };
@AnneRanch That output does not match the code. You must be iterating over a different array or
verify
in the loops is not the same variable as the one in the array declaration.Where do you get the
verify
variable? Is it still from the function argument
ProcessCommand ( char* command, QStringList verify[] )
?
What do you pass to that function when you call it? Maybe you passed a pointer to second element by mistake.Put this just before the first loop:
qDebug() << verify[0] << verify[1] << verify[2];
to make sure the contents of the array are what you think they are, because it looks like the array you are actually iterating over doesn't have that first list in it.
-
@AnneRanch That output does not match the code. You must be iterating over a different array or
verify
in the loops is not the same variable as the one in the array declaration.Where do you get the
verify
variable? Is it still from the function argument
ProcessCommand ( char* command, QStringList verify[] )
?
What do you pass to that function when you call it? Maybe you passed a pointer to second element by mistake.Put this just before the first loop:
qDebug() << verify[0] << verify[1] << verify[2];
to make sure the contents of the array are what you think they are, because it looks like the array you are actually iterating over doesn't have that first list in it.
@Chris-Kawa ```
Found it and as expected it was a very stupid mistake.
I have changed the array to a class available, but I was passing it indexed - as local variable - to my function.
Hence when I passed index 1 it became FIRST entry - 0 - in my test iteration...
Chris , thanks very much for all you help.
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
"void MainWindow::menu_slot_indexed(int)"
"add it as subwindow "
"Run bluetootctl... \n void MainWindow_Bluetooth::on_actionBluetooth_service_enable_triggered()"
"void MainWindow_Bluetooth::Process_Menu_Index(int)"
("Controller", "Agent registered") ("Discovery started", "SPP_CA") ("Controller", "Agent registered")
"1"
"Process command...QString BT_Utility_Library::ProcessCommand_Index(char *, QStringList *, int)"
("Discovery started", "SPP_CA") ("Controller", "Agent registered") ()
"void MainWindow_Bluetooth::Process_Menu_Index(int)"