+-

参见英文答案 > Where and why do I have to put the “template” and “typename” keywords? 6个
我正在尝试使用gcc 4.2在Linux上编译以下代码:
我正在尝试使用gcc 4.2在Linux上编译以下代码:
#include <map>
#include <list>
template<typename T>
class A
{
...
private:
std::map<const T, std::list<std::pair<T, long int> >::iterator> lookup_map_;
std::list<std::pair<T, long int> > order_list_;
};
当我编译这个类时,我从gcc收到以下消息:
error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
error: expected a type, got ‘std::list<std::pair<const T, long int>,std::allocator<std::pair<const T, long int> > >::iterator’
error: template argument 4 is invalid
我删除了文件名和行号,但它们都引用了声明地图的行.
当我用int或某些具体类型替换这些表达式中的对时,它编译得很好.有人可以向我解释我做错了什么.
最佳答案
您需要在std :: list< ...> :: iterator之前编写typename,因为迭代器是嵌套类型而您正在编写模板.
编辑:没有typename,GCC假定(作为标准要求)迭代器实际上是列表中的静态变量,而不是类型.因此“参数类型不匹配”错误.
点击查看更多相关文章
转载注明原文:模板C类声明中的类型/值不匹配 - 乐贴网