我有以下模板方法,
template <class T>
void Class::setData( vector<T> data )
{
vector<T>::iterator it;
}
我收到以下编译错误 (XCode/gcc)
error: expected `;' before 'it'
我发现其他人也有类似的问题here (read down to see it's the same even though it starts out with a different issue)但他们似乎已经通过更新 Visual Studio 解决了。这让我猜测这是一个编译器问题,它应该编译,对吗?通过从 0 到 size 的索引进行迭代是可行的,但这不是我更愿意实现此功能的方式。还有另一种解决方法吗? 谢谢
最佳答案
何时使用 typename
关键字的经典案例。希望你有 #include
-ed vector
和 iterator
并且有一个 using namespace std;
在范围内的某个地方。使用:
typename vector<T>::iterator it;
查找从属名称。开始here .
关于c++ - 带有自定义模板的 STL 迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/594521/