estoy tratando de utilizar el C++ de la biblioteca estándar algoritmo find
así:¿Qué hay de malo en mi uso del buscador de la biblioteca estándar de C++?
template<class T>
const unsigned int AdjacencyList<T>::_index_for_node(
const std::vector<T>& list, const T& node
) throw(NoSuchNodeException)
{
std::vector<T>::iterator iter = std::find(list.begin(), list.end(), node);
}
Cuando intento compilar, obtengo estos errores:
In file included from ../AdjacencyList.cpp:8:
../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&)’:
../AdjacencyList.h:99: error: expected ‘;’ before ‘iter’
../AdjacencyList.h:100: error: ‘iter’ was not declared in this scope
In file included from ../AdjacencyListTest.cpp:9:
../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&)’:
../AdjacencyList.h:99: error: expected ‘;’ before ‘iter’
../AdjacencyList.h:100: error: ‘iter’ was not declared in this scope
../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&) [with T = int]’:
../AdjacencyList.h:91: instantiated from ‘const std::vector<T, std::allocator<_Tp1> > Graph::AdjacencyList<T>::neighbours(const T&) [with T = int]’
../AdjacencyListTest.cpp:18: instantiated from here
../AdjacencyList.h:99: error: dependent-name ‘std::vector::iterator’ is parsed as a non-type, but instantiation yields a type
../AdjacencyList.h:99: note: say ‘typename std::vector::iterator’ if a type is meant
me siento como el "nombre-dependiente 'std :: vector :: iterator' se analiza como no-type, pero la instanciación produce un tipo "bit tiene la clave para entender lo que estoy haciendo mal, pero mi guisante no puede extraer el significado.
Actualización: que necesitaba para añadir un typename
, de acuerdo con la respuesta aceptada, y también utilizar un const_iterator
, por lo que la línea de código problemática se convirtió en:
typename std::vector<T>::const_iterator iter = std::find(list.begin(), list.end(), node);
No hay 'std :: find' en el STL. El espacio de nombres 'std' denota el uso de la Biblioteca estándar de C++. Mientras tanto, sacar el uso de 'std :: find' habría reducido la pregunta un poco; en este caso, tiene un nombre dependiente. –
Uno de los pocos casos en que las * últimas * líneas del mensaje de error contienen la información más útil ... – sth
Uno de los pocos casos en que un mensaje de error de un compilador de C++ en realidad contiene una sugerencia práctica y útil. ;-) –