0

I created a map of a pair and long long int -

map< pair< long long int, long long int >,long long int >; 

and an interator -

map< pair< long long int, long long int >, long long int >::iterator it1;

and when I did it1=same.begin() the compiler is throwing an error, why is this so ?

Error generated:

no viable overloaded '=' 
it1=same12.begin();
~~~^~~~~~~~~~~~~~~

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:648:29: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from '__map_iterator<__tree_iterator<__value_type, [...]>, __node_pointer, [...]>>' to 'const __map_iterator<__tree_iterator<__value_type, [...]>, std::__1::__tree_node, long long>, void *> *, [...]>>' for 1st argument class _LIBCPP_TYPE_VIS_ONLY __map_iterator

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
user3111412
  • 177
  • 1
  • 3
  • 11

1 Answers1

3

The error tells you:

no known conversion from '__map_iterator<...>' to 'const __map_iterator<_...>'

Notice the const.

Here, it looks like same12 is a const map (or a const map&, or perhaps it's being used inside a const method in which case const-correctness is being applied). This means that .begin() will return const_iterator, not iterator.

You need to change the type of it1 to a const_iterator instead of an iterator. The full type should be: map<pair<long long int, long long int>, long long int>::const_iterator.

Or just use auto it1 = same12.begin();.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144