views:

127

answers:

2

Following code is giving compilation error in visual studio 2009.

#include <iterator>
#include <vector>

template <class T1, class T2 >
class A
{
public:

    typename std::vector<std::pair<T1,T2> >::iterator iterator;
    std::pair<iterator, bool > foo(const std::pair<T1 ,T2> &value_in);
};

can anyone throw some light on it? Here is the error.

error C2327: 'A<T1,T2>::iterator' : is not a type name, static, or enumerator
A: 

you need typedef, not typename

J-16 SDiZ
+8  A: 

This declares iterator to be a variable (not a type):

typename std::vector<std::pair<T1,T2> >::iterator iterator;

Did you mean this?

typedef typename std::vector<std::pair<T1,T2> >::iterator iterator;

Further information: If you're curious about what typename does, read up about the differences between dependent and non-dependent names. If your type is closely related to a specific container, a typedef of that container can be useful, as the STL pattern uses many nested typedefs you can easily access (V::value_type below). This has the added advantage of requiring less change as your code evolves, e.g. using a different allocator (the second template parameter to vector), requires just one edit.

template<class T1, class T2>
struct A {
private:
  // you may or may not want to expose these convenience types
  typedef std::pair<T1, T2> P;
  typedef std::vector<P> V;

public:
  typedef typename V::value_type value_type;
  typedef typename V::iterator iterator;
  std::pair<iterator, bool> foo(value_type const& value_in);
};
Roger Pate