不能在从std :: vector继承的类中输入typedef迭代器

时间:2012-11-06 19:41:05

标签: c++ visual-studio-2010 templates vector

  

可能重复:
  Why do I need to use typedef typename in g++ but not VS?

我正在定义一个模板类,如下所示

template <class T>
class MyVector : public std::vector<boost::shared_ptr<T>>
{
public:
   typedef MyVector::iterator MyIter;
};

我在MyIter

的typedef中收到此错误
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

我正在使用VS 2010进行编译,我已确保存在vectorboost::shared_ptr的包含。如果我删除模板T并将其替换为int,则所有内容都会编译而不会出错。

我错过了什么?我想定义一个模板类并输入dedef。

1 个答案:

答案 0 :(得分:4)

这个问题在这里得到了很好的回答: Why do I need to use typedef typename in g++ but not VS?

但简而言之,您必须以这种方式使用关键字 typename

typedef typename MyVector::iterator MyIter;

因为在第一遍(验证模板语法)中,编译器并不真正知道 MyVector :: iterator 是一个类型还是一个变量(它实际上将在专用模板上)。所以暗示它是一种类型修复了这个问题。