问题描述
我正在尝试使用gcc 4.2在Linux上编译以下代码:
I am trying to compile the following code on Linux using gcc 4.2:
#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的以下消息:
When I compile this class I receive the following message from 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
我已删除文件名称和行号,但是它们都引用声明地图的行。
I have removed file names and line numbers , but they all refer to the line declaring the map.
当我将这些表达式中的对替换为int或某些具体类型时,编译起来就很好。有人可以告诉我我在做什么错。
When I replace the pair in these expressions with an int or some concrete type, it compiles fine. Can someone please explain to me what I am doing wrong.
推荐答案
您需要输入 typename
在 std :: list< ...> :: iterator
之前,因为 iterator
是一个
You need to write typename
before std::list<...>::iterator
, because iterator
is a nested type and you're writing a template.
编辑:没有 typename
,GCC假定(作为标准要求),迭代器
实际上是列表
中的静态变量,而不是类型。因此出现参数类型不匹配错误。
without the typename
, GCC assumes (as the standard requires) that iterator
is actually a static variable in list
, rather than a type. Hence the "parameter type mismatch" error.
这篇关于模板C ++类声明中的类型/值不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!