Problem with iterators and conts

E

Exits Funnel

Hello,

I've got the following code:

//Begin foo.cpp
#include <set>
#include <vector>

class sq_t
{
int m_priority;
public:
void ClearPriority() { m_priority = 0; }
};

typedef std::set<sq_t> its_t;
typedef std::vector<sq_t> sq_seq_t;

class TDM_t
{
public:
typedef std::set<sq_t> its_t;
its_t m_IT;
};

int main( )
{
TDM_t tdm;

for (its_t::iterator i_it = tdm.m_IT.begin();
i_it != tdm.m_IT.end(); ++i_it )
{
(*i_it).ClearPriority(); //this is line 29
}
}
//End foo.cpp

The compiler (g++ 3.2.3) complains:

foo.cpp: In function `int main()':
foo.cpp:29: passing `const sq_priority_t' as `this' argument of `void
sq_priority_t::ClearPriority()' discards qualifiers

I think that I understand the problem which is related to this line:

typedef typename _Rep_type::const_iterator iterator;

in stl_set.h

I'm pretty sure I need to use const_cast to solve the problem but I
can't seem to get the syntax right. Help!

-exits
 
V

Victor Bazarov

Exits Funnel said:
[...] Help!

Changing a set element may screw up the relative order of things in
the set without letting the set know so it could reorder them. It
is generally unsafe to change values in the set through the set's
iterators, that's why in some implementations it's a const_iterator.
The right way would be to remove the old element and insert another
one.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top