Help with STL const_iterators

Hi

I am upgrading from VC++ 98 to VC++ 2010 Express and apearently there have been some changes to the compiler and STL coding, which are causing me problems.

The code compiled with the old compiler, but with 2011 version i am always getting the Error message
"Cant assign somthihg to a const value"

The error appears in the second last line of the code below because of the increment(++): res.first->num)++

The code looks like this:


class count_t
{public:
dat_type leaf; short num; unsigned short dist; short done;
count_t(dat_type l, short n, short d=-1){leaf=l;num=n;dist=d;done=0;};
};

class less_count : public std::binary_function<const count_t&, const count_t&, bool>
{public:
bool operator()(const count_t& i1,const count_t& i2)const
{return &*(i1.leaf)<&*(i2.leaf);};
};

class count : public set<count_t,less_count>
{public:
iterator add(Leaf::dat_type dat)
{ pair<iterator,bool> res = insert(count_t(dat,1));
if (!res.second && !res.first->done) (res.first->num)++;
return res.first;
};
};

The Error is (unfortunately in german):
c:\users\andi\my documents\c\ki_later\ki-data.h(59): error C3892: "std::_Tree_unchecked_const_iterator<_Mytree,_Base>::operator ->": Einer Variablen, die konstant ist, kann nichts zugeordnet werden.
with
[
_Mytree=std::_Tree_val<std::_Tset_traits<Finder::count_t,Finder::less_count,std::allocator<Finder::count_t>,false>>,
_Base=std::_Iterator_base
]


The compiler clains res.first->num is a const value, but neither num is defined const, nor shoud insert return a const value.
What am I doing wrong???

P.S. I already tried without all the cons in class less_count, but that doesnt change a thing.

Thanks for ur help
Yes, a lot of compiler bugs were fixed in the 2010 version.

Set keys are read only. However you're not using num in the predicate, so it's okay to make it

mutable short num;

PS: don't inherit from std::set (or any other C++ container class), make it a member.
Last edited on
Hi and thanks for ur answer.
Unfortunately I dont quite understand it.
I declared num mutabele and it worked! I just dont get why i have to do it. none of the functions i apply is const, no?

And whats the disadvantage of inheriting from stl containers?

Thks a lot, Andi
Ok i get it now.
As u said the keys of a set are somehow made readonly (by making te iterator a const_iterator?)
which makes sense too.

is there a way of changing a const_iterator to an iterator?
Topic archived. No new replies allowed.