How to get class objects stored in a list in C++? -
i've defined own class , stored objects of them in std:list. want pick elements, went wrong - hope not complicated read:
std::map < long, firstclass*> firstclassmap; std::map < long, firstclass* >::iterator it; it=this->firstclassmap.begin() //initialization of firstclassmap somewhere else , shouldn't matter. list<secondclass*>::iterator listitem; list<secondclass*> deplist = it->second->getsecondclasslist(); for(listitem = deplist.begin(); listitem != deplist.end(); ++listitem) { /* -- error in line -- */ firstclass* theobject = listitem->getthelistobject(); std::cout << theobject->name(); }
then there function:
secondclass::getthelistobject() { return this->theobject; //returns firstclass object } firstclass::name() { return this->name //returns string }
here error
method 'getthelistobject' not resolved
and
error:element request »getthelistobject« in »* listitem.std::_list_iterator<_tp>::operator->()«, pointer type »secondclass*« (maybe »->« meant)
(i'm sorry, can't give correct error message. have translate german english, don't these in english)
i don't see problem. has idea?
kind regards
in code, listitem
isn't instance of secondclass*
, it's instance of iterator of secondclass*
. have dereference iterator access underlying object. loop should like:
for(listitem = deplist.begin(); listitem != deplist.end(); ++listitem) { firstclass* theobject = (*listitem)->getthelistobject(); //dereference iterator, //then call method. std::cout << theobject->name(); }
Comments
Post a Comment