EDITED Using template in C++ - run time error
-
I am trying hard to understand C++ templates. The following test code works as expected in class constructor. When I add SAME code to class method - which is is activated via an event - I get "index" out of range error. The error goes away when I include
QList information_main = QBluetoothLocalDevice::allDevices();
= QBluetoothLocalDevice::allDevices();
in the method. If the
QList information_main;
is "class variable" why do I have to "initialize " it again in the method?
The "index" is method variable (local) and is initialized to 0. - So why am I running out of index range ?
If you kindly answer the above and refrain from critiquing my coding style I would be grateful.
Thanks
// task retrive all locla BT devices
// changed to class variable
QList information_main = QBluetoothLocalDevice::allDevices();
qDebug() << "QDEBUG TRACE //11/23/2020 # of local BT detetced " << information_main.count();// add to display // add test text - output //on\_pushButton\_7\_clicked() ui->listWidget->addItem("MainWindow test # of local BT detetced "); ui->listWidget->addItem("# of local BT detetced "); ui->listWidget->addItem("information\_main.count())"); //TDOD add conversion ui->listWidget->addItem("TODO add conversion"); // ui->listWidget->addItem( fromStdString(information\_main.count()); //information\_main.first(); // display all adapters int index = 0; do { information\_main.at(index).address().toString(); ui->listWidget->addItem("information\_main.at(0).address()"); ui->listWidget->addItem( information\_main.at(index).address().toString()); ui->listWidget->addItem(" information\_main.at(0).name()"); ui->listWidget->addItem( information\_main.at(index).name()); index++; } while( index != information\_main.count() ); qDebug() << "file " << \_\_FILE\_\_; qDebug() << "function "<<\_\_FUNCTION\_\_; qDebug() << "@line " << \_\_LINE\_\_; qDebug()<<"TEMPORARY EXIT "; information\_main.count(); //exit(99);
#endif
EDITED
The purpose of the post is to identify WHY
the do-while loop works as coded in constructor and fails - run time error - when used in method.It is , as already posted , a test code.
The task is to read the list of devices returned byTemp
This thread is not very productive. Please do not waste your time "taking side trips" , perhaps re-read the original EDITED post to concentrate on the real issue. I am using the templates wrong and have no idea why. Thanks for understanding.
-
This thread is not very productive. Please do not waste your time "taking side trips" , perhaps re-read the original EDITED post to concentrate on the real issue. I am using the templates wrong and have no idea why. Thanks for understanding.
-
I am trying hard to understand C++ templates. The following test code works as expected in class constructor. When I add SAME code to class method - which is is activated via an event - I get "index" out of range error. The error goes away when I include
QList information_main = QBluetoothLocalDevice::allDevices();
= QBluetoothLocalDevice::allDevices();
in the method. If the
QList information_main;
is "class variable" why do I have to "initialize " it again in the method?
The "index" is method variable (local) and is initialized to 0. - So why am I running out of index range ?
If you kindly answer the above and refrain from critiquing my coding style I would be grateful.
Thanks
// task retrive all locla BT devices
// changed to class variable
QList information_main = QBluetoothLocalDevice::allDevices();
qDebug() << "QDEBUG TRACE //11/23/2020 # of local BT detetced " << information_main.count();// add to display // add test text - output //on\_pushButton\_7\_clicked() ui->listWidget->addItem("MainWindow test # of local BT detetced "); ui->listWidget->addItem("# of local BT detetced "); ui->listWidget->addItem("information\_main.count())"); //TDOD add conversion ui->listWidget->addItem("TODO add conversion"); // ui->listWidget->addItem( fromStdString(information\_main.count()); //information\_main.first(); // display all adapters int index = 0; do { information\_main.at(index).address().toString(); ui->listWidget->addItem("information\_main.at(0).address()"); ui->listWidget->addItem( information\_main.at(index).address().toString()); ui->listWidget->addItem(" information\_main.at(0).name()"); ui->listWidget->addItem( information\_main.at(index).name()); index++; } while( index != information\_main.count() ); qDebug() << "file " << \_\_FILE\_\_; qDebug() << "function "<<\_\_FUNCTION\_\_; qDebug() << "@line " << \_\_LINE\_\_; qDebug()<<"TEMPORARY EXIT "; information\_main.count(); //exit(99);
#endif
EDITED
The purpose of the post is to identify WHY
the do-while loop works as coded in constructor and fails - run time error - when used in method.It is , as already posted , a test code.
The task is to read the list of devices returned byTemp
Member 14980433 wrote:
Template "list" = QBluetoothLocalDevice::allDevices(); It would be nice if contributions lead to resolve this SPECIFIC problem. Naming variables, verifying validity of "loop index / count" , pointing out the code is repeated in class method so far does not addresses my error usage of template.
Did you use the Debugger to check what this
QBluetoothLocalDevice::allDevices();
returns?
-
I am trying hard to understand C++ templates. The following test code works as expected in class constructor. When I add SAME code to class method - which is is activated via an event - I get "index" out of range error. The error goes away when I include
QList information_main = QBluetoothLocalDevice::allDevices();
= QBluetoothLocalDevice::allDevices();
in the method. If the
QList information_main;
is "class variable" why do I have to "initialize " it again in the method?
The "index" is method variable (local) and is initialized to 0. - So why am I running out of index range ?
If you kindly answer the above and refrain from critiquing my coding style I would be grateful.
Thanks
// task retrive all locla BT devices
// changed to class variable
QList information_main = QBluetoothLocalDevice::allDevices();
qDebug() << "QDEBUG TRACE //11/23/2020 # of local BT detetced " << information_main.count();// add to display // add test text - output //on\_pushButton\_7\_clicked() ui->listWidget->addItem("MainWindow test # of local BT detetced "); ui->listWidget->addItem("# of local BT detetced "); ui->listWidget->addItem("information\_main.count())"); //TDOD add conversion ui->listWidget->addItem("TODO add conversion"); // ui->listWidget->addItem( fromStdString(information\_main.count()); //information\_main.first(); // display all adapters int index = 0; do { information\_main.at(index).address().toString(); ui->listWidget->addItem("information\_main.at(0).address()"); ui->listWidget->addItem( information\_main.at(index).address().toString()); ui->listWidget->addItem(" information\_main.at(0).name()"); ui->listWidget->addItem( information\_main.at(index).name()); index++; } while( index != information\_main.count() ); qDebug() << "file " << \_\_FILE\_\_; qDebug() << "function "<<\_\_FUNCTION\_\_; qDebug() << "@line " << \_\_LINE\_\_; qDebug()<<"TEMPORARY EXIT "; information\_main.count(); //exit(99);
#endif
EDITED
The purpose of the post is to identify WHY
the do-while loop works as coded in constructor and fails - run time error - when used in method.It is , as already posted , a test code.
The task is to read the list of devices returned byTemp
Could you post your class definition and the whole of your method?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Member 14980433 wrote:
Template "list" = QBluetoothLocalDevice::allDevices(); It would be nice if contributions lead to resolve this SPECIFIC problem. Naming variables, verifying validity of "loop index / count" , pointing out the code is repeated in class method so far does not addresses my error usage of template.
Did you use the Debugger to check what this
QBluetoothLocalDevice::allDevices();
returns?
Why complicate things?? Is it not sufficient that I get run time error? Running debug is still not addressing the issue. Logically - if the "list count " is > 0 the code in constructor works. I did verify the count > 0 before do-while loop , but that is superficial. If same code is used in method it appears that the count changed - the run time error said so. Hence using debugger will just confirm that - so what is the point? ( another useless side trip, in my opinion, proving nothing ). I will repeat - did I used the template QList wrong is my concern? How did the contents of the list changed AKA index is no longer valid ? I will delete the code in constructor and run only the method code. That is my next best guess, but it will NOT answer the problem.
-
Could you post your class definition and the whole of your method?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
Not sure if I want to do so. So far the discussion is centered on "why are you doing this ?" instead of on resolution. My code is written for me and I have had bad experience of "gurus" criticizing my style. Do not want that again. Let me try my last idea - running just the method and I'll try to sanitize / remove my comments from he code. Fair?
-
Why complicate things?? Is it not sufficient that I get run time error? Running debug is still not addressing the issue. Logically - if the "list count " is > 0 the code in constructor works. I did verify the count > 0 before do-while loop , but that is superficial. If same code is used in method it appears that the count changed - the run time error said so. Hence using debugger will just confirm that - so what is the point? ( another useless side trip, in my opinion, proving nothing ). I will repeat - did I used the template QList wrong is my concern? How did the contents of the list changed AKA index is no longer valid ? I will delete the code in constructor and run only the method code. That is my next best guess, but it will NOT answer the problem.
Member 14980433 wrote:
Running debug is still not addressing the issue.
Of course it is. That is what the debugger provides: information about why your program crashes, raises an exception, produces the wrong output etc. But if you want to waste your time complaining because we cannot magically produce the answer that you think you deserve, despite having only half the picture, that is up to you.
-
Why complicate things?? Is it not sufficient that I get run time error? Running debug is still not addressing the issue. Logically - if the "list count " is > 0 the code in constructor works. I did verify the count > 0 before do-while loop , but that is superficial. If same code is used in method it appears that the count changed - the run time error said so. Hence using debugger will just confirm that - so what is the point? ( another useless side trip, in my opinion, proving nothing ). I will repeat - did I used the template QList wrong is my concern? How did the contents of the list changed AKA index is no longer valid ? I will delete the code in constructor and run only the method code. That is my next best guess, but it will NOT answer the problem.
It absolutely is the issue. The debugger is there to debug YOU, not the code. It's there to show you what the code is really doing compared to what you THINK it does.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Why complicate things?? Is it not sufficient that I get run time error? Running debug is still not addressing the issue. Logically - if the "list count " is > 0 the code in constructor works. I did verify the count > 0 before do-while loop , but that is superficial. If same code is used in method it appears that the count changed - the run time error said so. Hence using debugger will just confirm that - so what is the point? ( another useless side trip, in my opinion, proving nothing ). I will repeat - did I used the template QList wrong is my concern? How did the contents of the list changed AKA index is no longer valid ? I will delete the code in constructor and run only the method code. That is my next best guess, but it will NOT answer the problem.
Member 14980433 wrote:
If same code is used in method it appears that the count changed - the run time error said so. Hence using debugger will just confirm that - so what is the point? ( another useless side trip, in my opinion, proving nothing ). I will repeat - did I used the template QList wrong is my concern? How did the contents of the list changed AKA index is no longer valid ?
Just debug your code step-by-step, look in the debug/watch window, compare the variables values with the ones you expected to see,... And you will find the problem!
-
Not sure if I want to do so. So far the discussion is centered on "why are you doing this ?" instead of on resolution. My code is written for me and I have had bad experience of "gurus" criticizing my style. Do not want that again. Let me try my last idea - running just the method and I'll try to sanitize / remove my comments from he code. Fair?
Don't know what to tell you, showing code is far better than trying to describe it. No one has criticized your coding 'style', they've mentioned things that are logical improvements, not code style improvements. Make the smallest runnable example that exhibits the problem, then show the whole example.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Don't know what to tell you, showing code is far better than trying to describe it. No one has criticized your coding 'style', they've mentioned things that are logical improvements, not code style improvements. Make the smallest runnable example that exhibits the problem, then show the whole example.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
Here is a working , sanitized / abbreviated TEST code used in method information_main.count() = 2
{
QList information_main = QBluetoothLocalDevice::allDevices();
// display all adapters
int index = 0;
do
{
information_main.at(index).address().toString();
index++;
} while( index != information_main.count() );reset index and run it again - works as expected
fails run time if index is not reset - as expectedindex = 0;
do
{
information_main.at(index).address().toString();
index++;
} while( index != information_main.count() ); -
Here is a working , sanitized / abbreviated TEST code used in method information_main.count() = 2
{
QList information_main = QBluetoothLocalDevice::allDevices();
// display all adapters
int index = 0;
do
{
information_main.at(index).address().toString();
index++;
} while( index != information_main.count() );reset index and run it again - works as expected
fails run time if index is not reset - as expectedindex = 0;
do
{
information_main.at(index).address().toString();
index++;
} while( index != information_main.count() );What is this? Can't you post the whole class definition, initialization, and the method you mentioned? :confused:
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Here is a working , sanitized / abbreviated TEST code used in method information_main.count() = 2
{
QList information_main = QBluetoothLocalDevice::allDevices();
// display all adapters
int index = 0;
do
{
information_main.at(index).address().toString();
index++;
} while( index != information_main.count() );reset index and run it again - works as expected
fails run time if index is not reset - as expectedindex = 0;
do
{
information_main.at(index).address().toString();
index++;
} while( index != information_main.count() );