我正在尝试创建一个模板函数,它接受任何类型 T 的 vector 参数(目前是原始类型)并打印出其内容:
template<class T>
void displayContents(const vector<T>& data)
{
vector<T>::const_iterator i;
i=data.begin();
for( ; i!=data.end(); i++){
cout<<*i<endl;
}
}
错误信息是:
In function 'void displayContents(const std::vector >&)': error: expected ';' before 'i' | error:'i' was not declared in this scope === Build finished: 2 errors, 0 warnings ===
我是否忽略了语法错误?
最佳答案
尝试以下操作:
typename vector<T>::const_iterator i;
正如 Björn 已经在评论中指出的那样,它是必需的,因为它是模板的从属名称。
关于c++ - 声明函数模板的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8471511/