简体   繁体   English

C ++编译错误

[英]c++ compilation error

Following code is giving compilation error in visual studio 2009. 以下代码在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

This declares iterator to be a variable (not a type): 这将iterator声明为变量(不是类型):

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. 更多信息:如果您想了解什么typename呢,读了大约之间的差异依赖和非依赖的名字。 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). 如果您的类型与特定的容器紧密相关,则该容器的typedef可能会有用,因为STL模式使用了许多嵌套的typedef,您可以轻松访问(下面的V::value_type )。 This has the added advantage of requiring less change as your code evolves, eg 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);
};

您需要typedef,而不是typename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM