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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Type of Derived Class a problem for STL lists?

Type of Derived Class a problem for STL lists?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++debuggingquestion
4 Posts 3 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.
  • J Offline
    J Offline
    Jordan C Atlas
    wrote on last edited by
    #1

    Hello, I have a project that crashes at run-time due to an Access Violation. The violation occurs in a section that looks something like this: double var; for (iter = ptrA->getPtrB()->getCList().begin(); iter != ptrA->getPtrB()->getCList().end(); iter++) { var = iter->getVar(); } A, B, and C are classes that have been defined elsewhere. iter is an iterator of type std::list. ptrA is a pointer to an object of type A. ptrB is a pointer to an object of type B. getCList is a method in the B class that returns a list of C objects. I have changed the names of the variables for simplicity. When I debug at run time it brings me to the line starting with "for". I cannot figure out where the problem is. My only hunch is that it might have something to do with the fact that the list that getCList() returns is actually NOT a list of C objects. Rather, it is a list of "D" objects where D is a class derived from C. Is that a problem? Does anyone else have anything else to suggest checking in the class definitions or anywhere else? I'd be happy to post more code if anyone thinks that would help... Thank you! -Jordan Atlas

    D L 2 Replies Last reply
    0
    • J Jordan C Atlas

      Hello, I have a project that crashes at run-time due to an Access Violation. The violation occurs in a section that looks something like this: double var; for (iter = ptrA->getPtrB()->getCList().begin(); iter != ptrA->getPtrB()->getCList().end(); iter++) { var = iter->getVar(); } A, B, and C are classes that have been defined elsewhere. iter is an iterator of type std::list. ptrA is a pointer to an object of type A. ptrB is a pointer to an object of type B. getCList is a method in the B class that returns a list of C objects. I have changed the names of the variables for simplicity. When I debug at run time it brings me to the line starting with "for". I cannot figure out where the problem is. My only hunch is that it might have something to do with the fact that the list that getCList() returns is actually NOT a list of C objects. Rather, it is a list of "D" objects where D is a class derived from C. Is that a problem? Does anyone else have anything else to suggest checking in the class definitions or anywhere else? I'd be happy to post more code if anyone thinks that would help... Thank you! -Jordan Atlas

      D Offline
      D Offline
      demf
      wrote on last edited by
      #2

      I can't figure it out because your codes are too complicated! I suggest you modify it like this: somelist listsome& = ptrA->getPtrB()->getCList(); for (iter = listsome.begin(); iter != listsome.end(); iter++) { var = iter->getVar(); } Because the first ptrA->getPtrB()->getCList() may be not as the same as your second ptrA->getPtrB()->getCList().

      1 Reply Last reply
      0
      • J Jordan C Atlas

        Hello, I have a project that crashes at run-time due to an Access Violation. The violation occurs in a section that looks something like this: double var; for (iter = ptrA->getPtrB()->getCList().begin(); iter != ptrA->getPtrB()->getCList().end(); iter++) { var = iter->getVar(); } A, B, and C are classes that have been defined elsewhere. iter is an iterator of type std::list. ptrA is a pointer to an object of type A. ptrB is a pointer to an object of type B. getCList is a method in the B class that returns a list of C objects. I have changed the names of the variables for simplicity. When I debug at run time it brings me to the line starting with "for". I cannot figure out where the problem is. My only hunch is that it might have something to do with the fact that the list that getCList() returns is actually NOT a list of C objects. Rather, it is a list of "D" objects where D is a class derived from C. Is that a problem? Does anyone else have anything else to suggest checking in the class definitions or anywhere else? I'd be happy to post more code if anyone thinks that would help... Thank you! -Jordan Atlas

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

        Hi Jordan, I assume that getCList is defined like so: list<obj> getCList() Instead of getCList returning a list, I suggest it returns a reference to the list. Meaning, your function will change to this: list<obj>& getCList() // just a small ampersand added... I'm sure if you make this change in the function declaration, the rest of your code will work just fine. Clinton

        J 1 Reply Last reply
        0
        • L Lost User

          Hi Jordan, I assume that getCList is defined like so: list<obj> getCList() Instead of getCList returning a list, I suggest it returns a reference to the list. Meaning, your function will change to this: list<obj>& getCList() // just a small ampersand added... I'm sure if you make this change in the function declaration, the rest of your code will work just fine. Clinton

          J Offline
          J Offline
          Jordan C Atlas
          wrote on last edited by
          #4

          Hello, Thank you both for your replies. It seems like you were both suggesting different flavors of the same idea. I tried both implementations and in both cases I still had a runtime error of "Access Violation". In this case, however, the debugger brought me to a spot in the file XTREE (which I assume is part of the STL?) instead of to the line in my code. Do you have any other suggestions? Also, I'm still wondering if the iterator is having a problem with the list I am retrieving being a dervied class rather than the class the iterator was created with. Thanks, -Jordan

          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