T::iterator 错误,其中模板参数 T 可能是 vector或列表
我正在尝试编写一个函数来打印常见 STL 容器(向量、列表等)的表示。我给了该函数一个模板参数 T,例如,它可能代表向量。我在获取类型 T 的迭代器时遇到问题
vector<int> v(10, 0);
repr< vector<int> >(v);
我
template <typename T>
void repr(const T & v)
{
cout << "[";
if (!v.empty())
{
cout << ' ';
T::iterator i;
for (i = v.begin();
i != v.end()-1;
++i)
{
cout << *i << ", ";
}
cout << *(++i) << ' ';
}
cout << "]\n";
}
......
brett@brett-laptop:~/Desktop/stl$ g++ -Wall main.cpp
main.cpp: In function ‘void repr(const T&)’:
main.cpp:13: error: expected ‘;’ before ‘i’
main.cpp:14: error: ‘i’ was not declared in this scope
main.cpp: In function ‘void repr(const T&) [with T = std::vector<int, std::allocator<int> >]’:
main.cpp:33: instantiated from here
main.cpp:13: error: dependent-name ‘T::iterator’ is parsed as a non-type, but instantiation yields a type
main.cpp:13: note: say ‘typename T::iterator’ if a type is meant
按照编译器的建议尝试了“typename T::iterator”,但只得到了一个更神秘的错误。
编辑:谢谢大家的帮助!对于想要使用此功能的任何人来说,这是一个工作版本:
template <typename T>
void repr(const T & v)
{
cout << "[";
if (!v.empty())
{
cout << ' ';
typename T::const_iterator i;
for (i = v.begin();
i != v.end();
++i)
{
if (i != v.begin())
{
cout << ", ";
}
cout << *i;
}
cout << ' ';
}
cout << "]\n";
}
I'm trying to write a function to print a representation of common STL containers (vector, list, etc..). I gave the function a template parameter T which, for example, might represent vector. I'm having problems getting an iterator of type T.
vector<int> v(10, 0);
repr< vector<int> >(v);
...
template <typename T>
void repr(const T & v)
{
cout << "[";
if (!v.empty())
{
cout << ' ';
T::iterator i;
for (i = v.begin();
i != v.end()-1;
++i)
{
cout << *i << ", ";
}
cout << *(++i) << ' ';
}
cout << "]\n";
}
...
brett@brett-laptop:~/Desktop/stl$ g++ -Wall main.cpp
main.cpp: In function ‘void repr(const T&)’:
main.cpp:13: error: expected ‘;’ before ‘i’
main.cpp:14: error: ‘i’ was not declared in this scope
main.cpp: In function ‘void repr(const T&) [with T = std::vector<int, std::allocator<int> >]’:
main.cpp:33: instantiated from here
main.cpp:13: error: dependent-name ‘T::iterator’ is parsed as a non-type, but instantiation yields a type
main.cpp:13: note: say ‘typename T::iterator’ if a type is meant
I tried 'typename T::iterator' as the compiler suggested, but only got a more cryptic error.
Edit: Thanks for the help guys! Here's a working version for anyone who wants to use this function:
template <typename T>
void repr(const T & v)
{
cout << "[";
if (!v.empty())
{
cout << ' ';
typename T::const_iterator i;
for (i = v.begin();
i != v.end();
++i)
{
if (i != v.begin())
{
cout << ", ";
}
cout << *i;
}
cout << ' ';
}
cout << "]\n";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要
typename
告诉编译器::iterator
应该是一种类型。编译器不知道它是一个类型,因为在实例化模板之前它不知道 T 是什么。例如,它还可以引用某些静态数据成员。这是你的第一个错误。您的第二个错误是
v
是对 const 的引用。因此,您必须使用::const_iterator
而不是::iterator
。您不能向常量容器请求非常量迭代器。You need
typename
to tell the compiler that::iterator
is supposed to be a type. The compiler doesn't know that it's a type because it doesn't know what T is until you instantiate the template. It could also refer to some static data member, for example. That's your first error.Your second error is that
v
is a reference-to-const. So, instead of::iterator
you have to use::const_iterator
. You can't ask a constant container for a non-const iterator.将
T::iterator i;
更改为typename T::const_iterator i;
因为::iterator
的类型为T
和v
是一个const &
。在限定依赖类型之前,您需要
typename
。如果没有
typename
,则 C++ 解析规则规定,合格的从属名称应解析为非类型
,即使它会导致语法错误。typename
声明后面的名称应被视为类型。否则,名称将被解释为引用非类型。Change
T::iterator i;
totypename T::const_iterator i;
because::iterator
is of typeT
andv
is aconst &
.Before a qualified dependent type, you need
typename
.Without
typename
, there is a C++ parsing rule that says that qualified dependent names should be parsed asnon-types
even if it leads to a syntax error.typename
states that the name that follows should be treated as a type. Otherwise, names are interpreted to refer to non-types.也许这会有所帮助:
Typename 在引用类型的限定依赖名称之前是强制性的,除非该名称命名基类或在初始化列表中。在模板中使用限定(但非依赖名称)之前,类型名称是可选的,但在命名基类或初始化列表时除外。
Maybe this will help:
Typename is mandatory before a qualified-dependent name which refers to a type, unless that name is naming a base class, or in an initialization list. Typename is optional before a qualified (but non-dependent name) is used within a template, except again when naming a base class or in an initialization list.