Search code examples
c++c++11iteratorc++98typename

Compiler Error: Expected Nested Name Specifier before typedef


In a template class with two instantiation types (T and PT) I currently have the following line for using a const_iterator for the class:

typedef typename std::vector< std::pair<T, PT> >::const_iterator const_iterator;

This works in a C++11/14 environment however when compiling in a C++98 environment (which may, unfortunately, be required for various reasons) I get the error shown in the title, expected nested name specifier before typedef.

Is there any way I can resolve this issue for old compilers?

EDIT: Here is the basic structure of my class,

template<typename T, typename PT> class MyClass {

private:

    std::vector< std::pair<T,PT> > dataWithPriorityVec;    

    //... various private methods...

public:

    typedef typename std::vector< std::pair<T,PT> >::const_iterator const_iterator;

    //... constructors and various public methods...

};

Solution

  • Are you including <vector> and <utility> before the class definition?

    You need to "include what you use" to avoid cross compilation issues.

    There is no guarantee that <utility> (required for std::pair), or <vector> are included in any of the other standard headers. As noted in the comments.

    So the compiler you use for the C++98 build could require it to compile correctly.