What is wrong with my usage of C++ standard library's find?

16,214
std::vector<T>::iterator iter = /* .... */; 

iterator is a dependent name (effectively, it depends on the type parameter T). Dependent names are assumed not to name types unless you use typename:

typename std::vector<T>::iterator iter = /* .... */;

For more, consider reading the Stack Overflow C++ FAQ entry "Where and why do I have to put “template” and “typename” on dependent names?"

You will also need to use const_iterator, since list is const-qualified. You should probably drop the exception specification as well; it's best to "never write an exception specification."

Share:
16,214
Josh Glover
Author by

Josh Glover

A Lisp fanatic hacking Clojure in Stockholm.

Updated on June 27, 2022

Comments

  • Josh Glover
    Josh Glover almost 2 years

    I'm trying to use the C++ standard library's find algorithm like this:

      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);
      }
    

    When I try to compile, I get these errors:

    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
    

    I feel like the "dependent-name ‘std::vector::iterator’ is parsed as a non-type, but instantiation yields a type" bit holds the key to understanding what I'm doing wrong, but my pea-brain cannot extract the meaning.

    Update: I needed to add a typename, as per the accepted answer, and also use a const_iterator, so the problematic line of code became:

        typename std::vector<T>::const_iterator iter = std::find(list.begin(), list.end(), node);
    
  • Josh Glover
    Josh Glover about 13 years
    Fantastic answer, James! Actually, it was more of a code review than an answer, but code reviews are always welcome. :)