Q:

How to get class objects stored in a list in C++?

I've defined my own class and stored objects of them in a std:list. Now I want to pick up all the elements, but something went wrong - I hope this is not too complicated to read:
std::map < long, FirstClass*> FirstClassMap;
std::map < long, FirstClass* >::iterator it;
it=this->FirstClassMap.begin() 
//initialization of FirstClassMap is somewhere else and shouldn't matter.

list<SecondClass*>::iterator ListItem;
list<SecondClass*> depList = it->second->getSecondClassList();

for(ListItem = depList.begin(); ListItem != depList.end(); ++ListItem)
{
    /* -- the error is in this Line -- */
    FirstClass* theObject = ListItem->getTheListObject();
    std::cout << theObject->Name();
}

Then there is the function:
SecondClass::getTheListObject()
{
    return this->theObject; //returns a FirstClass object
}

FirstClass::Name()
{
    return this->name //returns a string
}

Here I get the Error

Method 'getTheListObject' could not be resolved

and

Error:element request »getTheListObject« in »*
  ListItem.std::_List_iterator<_Tp>::operator->()«, whose
  pointer type is »SecondClass*« (maybe  »->« was meant)

(I'm sorry, that I can't give you the correct error message. I have to translate it from German to English, I don't get these in English)
I don't really see the problem. Has anyone an idea?
Kind Regards

A:

In your code, ListItem isn't an instance of SecondClass*, it's an instance of an iterator of SecondClass*. You have to dereference the iterator to get access to the underlying object. So your for loop should look like:
for(ListItem = depList.begin(); ListItem != depList.end(); ++ListItem)
{
    FirstClass* theObject = (*ListItem)->getTheListObject(); //Dereference the iterator, 
                                                             //then call the method.
    std::cout << theObject->Name();
}

