g++ 编译器给出了这个错误: expected `;'在“它”之前
template <typename T>
class myList : public std::list<T>
{
public:
void foo ()
{
std::list<T>::iterator it; // compiler error as above mentioned, why ???
}
};
谢谢。
最佳答案
在 g++ 中。每当在模板中看到错误时:
error: expected ';' before 'it'
怀疑你需要一个类型名:
typename std::list<T>::iterator it;
当您在模板中声明了一个依赖于一个或多个模板参数的新类型(在本例中为列表迭代器)时,这是必需的。这种需求并不是 g++ BTW 独有的,它是标准 C++ 的一部分。
关于C++ list<T>::iterator 不能在派生类模板中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2539709/