const iterator error
hi,
I am in windows xp sp2 and the compiler is GNU Make 3.80 (gcc 3.4.2)
I am trying to use a multiset iterator to update the content of each element in a multiset. The content is a vector of pointer.
Here is the partial code:
Entity.h
---
class Trade{
...
};
class Meeting{
...
vector<Trade*> trades;
...
};
And in global, I have code like this:
main.cpp
---
multiset<Meeting> meetings;
void update_meetings()
{
multiset<Meeting>::iterator it;
for( it = meetings.begin ; it != meetings.end(); it++ )
{
(it->trades).push_back( new Trade() );
}
}
int main()
{
(read data and create meetings)
...
update_meetings();
}
Then I got a compiler error like this
error: passing `const std::vector<Trade*, std::allocator<Trade*> >' as `this' argument of
`void std::vector<_Tp, _Alloc>::push_back(const _Tp&)
[with _Tp = Trade*, _Alloc = std::allocator<Trade*>]' discards qualifiers
The reason I want to initialize multiset<Meeting> first, then update it later is that it has to wait for another data to be read in. I guess I could integrate the update into the intialization. But I want to figure out where this error comes from. Seems like it has no problem in vc 2005.
does anybody have any idea?
[1506 byte] By [
firegun9] at [2007-11-20 11:13:16]

# 1 Re: const iterator error
Most compilers will not let you modify elements in a set
(because you could potentially change the order).
The only portable way to update an element in a set is to
erase the element first and re-insert it after making your
modifications.
You might consider changing your design slightly to use a
multimap. The "key" would be your current Meeting class
(with out the vector in it). The "value" would be the vector.
# 2 Re: const iterator error
A lot of the problem with the lack of "concepts" is that errors in using the standard library can be difficult to trace because the compiler will often show it as an error in the standard library.
multiset allows duplicates but it has similar concepts as set in that the "key" element is constant, which in the case of set is the element itself.
I also do not see exactly how your meetings are sorted, but given that there may be a property there somewhere, I will suggest you separate this away from the vector, and maybe use multimap instead.
Also your class Meeting is copyable, which actually is fine except that
1. it can be expensive to copy a vector
and (more importantly)
2. How are you going to keep trace of the pointers to Trade that you allocated with new. At some point you need to delete them.
A better suggestion is to use vector< shared_ptr< Trade > >. You may wish to use shared_ptr< Meeting > as well (as the value part of your multimap).