#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    179
    Rep Power
    28

    Standard library: list problem


    I have a C++ standard library list.
    When I am trying to iterate over the list, like this:

    Iterator iter = list->begin();

    I get an exception and a core dump.
    The line where it occurs is line 365 in the file
    stl_list.h:

    iterator
    begin()
    { return static_cast<_Node*>(_M_node->_M_next); }

    Does anyone know what's going on here?
    Thanks in advance.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Please show some of the exact code you use for declaring the list and the iterator. They are mostly voodoo to me, but I have used them extensively and I don't think what you are showing will compile.

    I would expect something like this:

    Code:
    #include <stdio.h>
    
    #include <list>
    
    int main(){
        std::list < std::string > myList;
    
        myList.push_back("first string");
        myList.push_back("second string");
        myList.push_back("3rd string");
        myList.push_back("4th string");
        myList.push_back("fifth string");
        myList.push_back("hex string");
    
        std::list < std::string >::iterator myIterator = myList.begin();
    
        while (myIterator != myList.end()){
            printf("%s\n", (*myIterator).c_str());
            myIterator++;
        }
    
        return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Hazarding a guess here, but has list actually been initialized?
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    The big question is an iterator to what? You can't write a declaration like "Iterator p = mylist.begin();" What you can do is "list<string>::iterator p = mylist.begin();"

    That's what I would guess your error is.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo