Initializing a set iterator in C++

1.3k views Asked by At

I just upgraded a third party library called RogueWave which I use for database functions.

But, I am getting a really silly compile error, and I am not quite sure what it is complaining about:

 typedef std::set< EVBusCalOverride > OverrideSet;
 OverrideSet overrides;

 OverrideSet::iterator itor = this->overrides.begin( );
 while( itor != this->overrides.end( ) )

I get an error on the iterator initialization line saying

An object or reference of type "std::set<EVBusCalOverride,std::less<EVBusCalOverride>,std::allocator<EVBusCalOverride> >::iterator" 
cannot be initialized with an rvalue of type    "std::_Tree<std::_Tset_traits<EVBusCalOverride,std::less<EVBusCalOverride>,std::allocator<EVBusCalOverride>,0> >::const_iterator".

And EVBusCalOverride is just a class

class EVBusCalOverride
{

   private:
   RWDate overrideDate;
   bool   businessDay;
};

RWDate is a datetype variable that belongs to the RogueWave libraries.

Am not sure how that affects the iterator initialization. Can anybody point me in the right direction of what might be causing this?

3

There are 3 answers

1
CashCow On BEST ANSWER

roguewave is a very old library.

Most collections have two types of iterator, iterator which allows you to change the members and const_iterator which does not.

std::set is an exception to that rule in a sense that you cannot modify the members so all iterators are const. However they still support iterator and const_iterator, often one being an alias of the other, but roguewave obviously doesn't.

Changing iterator in your code to const_iterator will probably fix, but I would suggest you change your version of STL if possible.

0
Peter Ruderman On

It looks like overrides is const. Is this code part of a const method? In that case, you need to initialize the iterator like so:

OverrideSet::const_iterator itor = this->overrides.begin( ); 
1
Olaf Dietsche On

The error message says:

... ::const_iterator

I guess this is called in a const method.