iterator problem

This segment of my code is giving me problems

1
2
3
4
5
6
7
8
9
		else
		{
			for (vector<Location>::iterator iter = neighbors.begin(); iter != neighbors.end(); iter++)
			{
				if (*iter == neighbors[rand_pos])
					return *iter;
			}
			neighbors.erase(iter);
		}


These are the errors I'm getting.

../worm.cpp:144: error: no match for 'operator==' in '(&iter)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Location*, _Container = std::vector<Location, std::allocator<Location> >]() == (&neighbors)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = Location, _Alloc = std::allocator<Location>](((unsigned int)rand_pos))'
../worm.cpp:147: error: name lookup of `iter' changed for new ISO `for' scoping
../worm.cpp:142: error:   using obsolete binding at `iter'


It seems like it is trying to tell me that iterators do not support the == operator. I think the problem is *iter == neighbors[rand_pos].
Line 5 looks okay to me, assuming Location has an operator==() it can use. Line 8 looks weird though; variables in for loops are scoped the loop itself and can't be accessed outside of it.
Last edited on
Fixed it. I forgot to overload the == operator for my Locations class.
Yea those 2 problems are exactly it. I just caught it after staring for about 20 minutes.. im pretty tired i guess.
Topic archived. No new replies allowed.