Modifying elements in a STL List - C++ -


i attempting construct binary search tree using generalized list in c++.

class element { private:        list<element*> _children;        char* _name; // , other data members/methods... } 

as can see, have class "element" , has list "_children" of element pointers.

i trying access these children may add children them , forth...

however, cannot modify these values current method of using "const_iterator" , reason doing "begin()" method of _children returns const_iterator.

someone help? thank :)

update: thank much... turns out, mistakenly had method return const reference of _children data member.

const list<element*>& getchildren();// return [_children] 

i deleted const , works perfect now. thank you! :d

the begin function return const_iterator if list const. _children list should able standard iterator let perform non-const operations on it:

list<element*>::iterator = _children.begin(); 

this won't work if passing off const reference list , trying non-const iterator that. not allowed:

void dosomething( const list<element*>& l ) {     list<element*>::iterator = l.begin(); } 

you need instead pass non-const reference list.

the other case not allowed in const function, i.e.

void dosomething() const {     list<element*>::iterator = _children.begin(); } 

but need see more of code confirm if you're doing or not.


Popular posts from this blog