Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. EDITED Using template in C++ - run time error

EDITED Using template in C++ - run time error

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasewpfdesigndebugging
30 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Member_14980433

    Thanks for the reply. Good point, but does not address the question. The do-while-loop compares the index to count of the items being processed. No count , no processing, Yes, I could check the count before entering the loop. I am sure I will find more "elegant" way to process the items and stop the loop.

    J Offline
    J Offline
    jeron1
    wrote on last edited by
    #7

    Member 14980433 wrote:

    No count , no processing,

    No, you're going to process it once before size is checked, and likely crash if the list is empty.

    Member 14980433 wrote:

    I am sure I will find more "elegant" way to process the items and stop the loop.

    Indeed, most likely a for loop.

    "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

    M 1 Reply Last reply
    0
    • M Member_14980433

      What you have done is to create a second local variable inside that method with the same name as the class level one

      So the variable in class takes precedence over the variable in method? Then why is the error saying the "index" is out of range? The "index" is local in constructor and also local in method.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #8

      Member 14980433 wrote:

      So the variable in class takes precedence over the variable in method?

      No, you cannot have two variables with the same name in scope at the same time.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      1 Reply Last reply
      0
      • M Member_14980433

        What you have done is to create a second local variable inside that method with the same name as the class level one

        So the variable in class takes precedence over the variable in method? Then why is the error saying the "index" is out of range? The "index" is local in constructor and also local in method.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #9

        No, the other way round. The variable declared in the method will be the one that is used, as it is in the innermost scope. But you should not be declaring the same variable at two scope levels. Just change your code as I suggested to correct the problem. As to the index problem, i suggest you change the do while to a simple while or for loop, to ensure you do not enter the loop if information_main.count() returns zero.

        M 1 Reply Last reply
        0
        • J jeron1

          Member 14980433 wrote:

          No count , no processing,

          No, you're going to process it once before size is checked, and likely crash if the list is empty.

          Member 14980433 wrote:

          I am sure I will find more "elegant" way to process the items and stop the loop.

          Indeed, most likely a for loop.

          "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

          M Offline
          M Offline
          Member_14980433
          wrote on last edited by
          #10

          No, you're going to process it once before size is checked, and likely crash if the list is empty.

          My objective is to identify the reason for the "out of range "error. I am getting same when accessing another template list. Ok, let's make an assumption the do-while " list " is indeed empty. ( It is not ) Would the compiler produce the "index out of range" error then ?

          L V 2 Replies Last reply
          0
          • L Lost User

            No, the other way round. The variable declared in the method will be the one that is used, as it is in the innermost scope. But you should not be declaring the same variable at two scope levels. Just change your code as I suggested to correct the problem. As to the index problem, i suggest you change the do while to a simple while or for loop, to ensure you do not enter the loop if information_main.count() returns zero.

            M Offline
            M Offline
            Member_14980433
            wrote on last edited by
            #11

            So in other words "index" in constructor and "index" in methods are independent , hence not source of the "out of range error". That should apply to any variable, including template. I understand that using same names could be buggy, but I need to verify WHAT is causing the "out of range error" and IMHO using DIFFERENT names should not be "real " solutions, unless something else is wrong with my implementation of templates. As I said - this is a test code and the "count" is used to simply verify the list content prior to doing the "do -while " loop and it is > 0. I have never used "iteration" but planning to employ it after I get the "out of range " error Solved.

            J L 2 Replies Last reply
            0
            • M Member_14980433

              So in other words "index" in constructor and "index" in methods are independent , hence not source of the "out of range error". That should apply to any variable, including template. I understand that using same names could be buggy, but I need to verify WHAT is causing the "out of range error" and IMHO using DIFFERENT names should not be "real " solutions, unless something else is wrong with my implementation of templates. As I said - this is a test code and the "count" is used to simply verify the list content prior to doing the "do -while " loop and it is > 0. I have never used "iteration" but planning to employ it after I get the "out of range " error Solved.

              J Offline
              J Offline
              jeron1
              wrote on last edited by
              #12

              Having this;

              QList information_main;

              inside the method creates a new Qlist type variable called 'information_main', this variable is totally different than the class variable called 'information_main' that has been initialized in a class constructor. This new QList variable (the one just declared inside the method) is empty because you just created it and have not added anything to it. When you make an indexed access to an empty QList variable it will fail. That's why you have to initialize (add stuff to) the new QList variable (the one just declared inside the method) to get the loop to work.

              "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

              1 Reply Last reply
              0
              • M Member_14980433

                So in other words "index" in constructor and "index" in methods are independent , hence not source of the "out of range error". That should apply to any variable, including template. I understand that using same names could be buggy, but I need to verify WHAT is causing the "out of range error" and IMHO using DIFFERENT names should not be "real " solutions, unless something else is wrong with my implementation of templates. As I said - this is a test code and the "count" is used to simply verify the list content prior to doing the "do -while " loop and it is > 0. I have never used "iteration" but planning to employ it after I get the "out of range " error Solved.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #13

                Yes, as I mentioned above the one in the innermost scope is the one that will be used; that is the one declared inside the method. But you really should not create variables with the same name like this, it just confuses people, particularly you. See my last sentence in the previous reply for a possible fix to the out of range error. But you should really capture the count first in a variable and test it to see if it returns a ositive value.

                M 1 Reply Last reply
                0
                • M Member_14980433

                  No, you're going to process it once before size is checked, and likely crash if the list is empty.

                  My objective is to identify the reason for the "out of range "error. I am getting same when accessing another template list. Ok, let's make an assumption the do-while " list " is indeed empty. ( It is not ) Would the compiler produce the "index out of range" error then ?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #14

                  It is not the compiler that produces the error, it is your code when it runs. You really need to make use of the debugger to find out exactly where the error occurs and what variable values are the cause.

                  1 Reply Last reply
                  0
                  • L Lost User

                    Yes, as I mentioned above the one in the innermost scope is the one that will be used; that is the one declared inside the method. But you really should not create variables with the same name like this, it just confuses people, particularly you. See my last sentence in the previous reply for a possible fix to the out of range error. But you should really capture the count first in a variable and test it to see if it returns a ositive value.

                    M Offline
                    M Offline
                    Member_14980433
                    wrote on last edited by
                    #15

                    I am not sure who is confused. Just read the original post and subsequent comments. So far it looks as the template code fails if there are NO items in it. That makes sense, but it is not the primary issue - just nice to know. However, the list is not empty and I still do not have an answer to the original post. Maybe I should rephrase the question. The template list content was created by other process, what I want is to read it - I do not want to rebuild / reinitialize it. I can read it fine first time in constructor, now I like to read it again in method. I do not want to reinitialize it. And I am doing it wrong , and the matching names is NOT the issue. I do not want to reinitialize it.

                    L 1 Reply Last reply
                    0
                    • M Member_14980433

                      I am not sure who is confused. Just read the original post and subsequent comments. So far it looks as the template code fails if there are NO items in it. That makes sense, but it is not the primary issue - just nice to know. However, the list is not empty and I still do not have an answer to the original post. Maybe I should rephrase the question. The template list content was created by other process, what I want is to read it - I do not want to rebuild / reinitialize it. I can read it fine first time in constructor, now I like to read it again in method. I do not want to reinitialize it. And I am doing it wrong , and the matching names is NOT the issue. I do not want to reinitialize it.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #16

                      Member 14980433 wrote:

                      I am not sure who is confused.

                      I am, because there is still some information missing. And I cannot understand why you need to read this information inside the constructor (which is definitely wrong), as well as in the method (which is correct). But to go back to the main point, the only way to resolve the index out of range problem is to use the debugger and step through the code as it runs.

                      1 Reply Last reply
                      0
                      • M Member_14980433

                        No, you're going to process it once before size is checked, and likely crash if the list is empty.

                        My objective is to identify the reason for the "out of range "error. I am getting same when accessing another template list. Ok, let's make an assumption the do-while " list " is indeed empty. ( It is not ) Would the compiler produce the "index out of range" error then ?

                        V Offline
                        V Offline
                        Victor Nijegorodov
                        wrote on last edited by
                        #17

                        Member 14980433 wrote:

                        Ok, let's make an assumption the do-while " list " is indeed empty. ( It is not )

                        Why are you sure it is non-empty? How did did you check it? Using debugger? Using TRACE with the item count? Using something else?

                        1 Reply Last reply
                        0
                        • M Member_14980433

                          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 by

                          Temp

                          M Offline
                          M Offline
                          Member_14980433
                          wrote on last edited by
                          #18

                          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.

                          L 1 Reply Last reply
                          0
                          • M Member_14980433

                            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.

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #19

                            Firstly this is not a template, it is (one assumes, since we cannot see you code) a class. And a number of us have explained at length what is wrong and how to fix it. If you think that is taking side trips ...

                            1 Reply Last reply
                            0
                            • M Member_14980433

                              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 by

                              Temp

                              V Offline
                              V Offline
                              Victor Nijegorodov
                              wrote on last edited by
                              #20

                              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?

                              M 1 Reply Last reply
                              0
                              • M Member_14980433

                                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 by

                                Temp

                                J Offline
                                J Offline
                                jeron1
                                wrote on last edited by
                                #21

                                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

                                M 1 Reply Last reply
                                0
                                • V Victor Nijegorodov

                                  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?

                                  M Offline
                                  M Offline
                                  Member_14980433
                                  wrote on last edited by
                                  #22

                                  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.

                                  L D V 3 Replies Last reply
                                  0
                                  • J jeron1

                                    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

                                    M Offline
                                    M Offline
                                    Member_14980433
                                    wrote on last edited by
                                    #23

                                    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?

                                    J 1 Reply Last reply
                                    0
                                    • M Member_14980433

                                      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.

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #24

                                      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.

                                      1 Reply Last reply
                                      0
                                      • M Member_14980433

                                        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.

                                        D Offline
                                        D Offline
                                        Dave Kreskowiak
                                        wrote on last edited by
                                        #25

                                        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

                                        1 Reply Last reply
                                        0
                                        • M Member_14980433

                                          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.

                                          V Offline
                                          V Offline
                                          Victor Nijegorodov
                                          wrote on last edited by
                                          #26

                                          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!

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups