"No match for operator =" trying to iterate over a map in C ++

I am trying to iterate over a map defined as follows:

std::map< size_type, std::pair<size_t, unsigned int> > ridx_; 

Now I am trying to iterate through ridx_ (which is a private member of the class) in the next friend function, which overloads operator <<

 std::ostream& operator<<(std::ostream &os, const SMatrix &m) { std::map< size_type, std::pair<size_t, unsigned int> >::iterator it; //The following is line 34 for (it = m.ridx_.begin(); it != m.ridx_.end(); it++) os << it->first << endl; return os; } 

However g ++ errors with:

SMatrix.cpp: 34: error: no match for 'operator =' in 'it = m-> SMatrix :: ridx_.std :: map <_Key, _Tp, _Compare, _Alloc> :: start with _Key = unsigned int, _Tp = std :: pair, _Compare = std :: less, _Alloc = std :: allocator → '/usr/include/c++/4.3/bits/stl_tree.h:152: note: candidates: std :: _ Rb_tree_iterator → & std :: _ Rb_tree_iterator → :: operator = (const std :: _ Rb_tree_iterator → &) make: * [myTest] Error 1

What am I doing wrong?

+4
source share
1 answer

Since m (and therefore m.ridx_ ) is constant, you should use std::map< size_type, std::pair<size_t, unsigned int> >::const_iterator , not ::iterator .

If you use the C ++ 0x compiler, you may need to use auto :

 for (auto it = m.ridx_.begin(); it != m.ridx_.end(); it++) 
+12
source

Source: https://habr.com/ru/post/1369603/


All Articles