iterator for vector of pointers to generic type won't compile

Bug #311663 reported by Mike Gashler
2
Affects Status Importance Assigned to Milestone
gcc-4.2 (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

This function iterates over a vector to find an element. It won't compile with g++ 4.2.4

#include <vector>
template<typename T>
size_t index_of(vector<T*>& v, T* p)
{
 for(std::vector<T*>::iterator it = v.begin(); it != v.end(); it++)
  if(p == *it)
   return (it - v.begin());
 return -1;
}

The error is:

    error: expected `;' before ‘it’

Visual C++ 2005 can compile this code. I know that doesn't always mean much, but in this case I think they are correct. If I am mistaken such that this code really should not compile, the issue is definitely not a missing semicolon before 'it', so at least the error message could be improved a bit.

Revision history for this message
Mike Gashler (gashlerm) wrote :

My mistake. Sorry. The solution is to change this line
 for(vector<T*>::iterator it = v.begin(); it != v.end(); it++)
to
 for(typename vector<T*>::iterator it = v.begin(); it != v.end(); it++)

Changed in gcc-4.2:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.