变化:
MyList::iterator it;
到:
typename MyList::iterator it;
我相信这有做不被肯定为MyList::iterator
是否应该是某种类型的值,编译器(比如说iterator
是MyList
的静态成员)或类型。 typename
强制后者(正确)选项。
我相信从这里开始相关标准的引用,第二个14.6:
在模板中使用的名字,假设除非适用的名称查找发现一个类型名称或名称由合格未命名类型关键字typename
。
所以,你必须弄清楚什么是“适用名称查找”是的,但标准也与此示例跟进:
// no B declared here
class X;
template<class T> class Y {
class Z; // forward declaration of member class
void f() {
X* a1; // declare pointer to X
T* a2; // declare pointer to T
Y* a3; // declare pointer to Y<T>
Z* a4; // declare pointer to Z
typedef typename T::A TA;
TA* a5; // declare pointer to T’s A
typename T::A* a6; // declare pointer to T’s A
T::A* a7; // T::A is not a type name:
// multiply T::A by a7
B* a8; // B is not a type name:
// multiply B by a8; ill-formed,
// no visible declaration of B
}
};
是的,你失去了一些东西重要:告诉我们的编译器错误是什么。 :) – 2010-10-26 14:35:38
@Steve:不,编译器无关紧要,只是g ++错误地接受了代码。 – 2010-10-26 14:43:43
是的,对不起'回合。编辑。 – 2010-10-26 14:49:11