可能重复:
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
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
我正在使用VS 2010进行编译,我已确保存在vector
和boost::shared_ptr
的包含。如果我删除模板T
并将其替换为int
,则所有内容都会编译而不会出错。
我错过了什么?我想定义一个模板类并输入dedef。
答案 0 :(得分:4)
这个问题在这里得到了很好的回答: Why do I need to use typedef typename in g++ but not VS?
但简而言之,您必须以这种方式使用关键字 typename :
typedef typename MyVector::iterator MyIter;
因为在第一遍(验证模板语法)中,编译器并不真正知道 MyVector :: iterator 是一个类型还是一个变量(它实际上将在专用模板上)。所以暗示它是一种类型修复了这个问题。