#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
template<class T>
void test_iter(const vector<T>);
int main()
{
vector<string> vec_str{ "Anthony","Chou","Jay","Hahaha" };
test_iter(vec_str);
system("pause");
return 0;
}
template<class T>
void test_iter(const vector<T> vec)
{
typename vector<T>::iterator iter;
for (iter = vec.begin(); iter != vec.end(); iter++)
{
cout << *iter << endl;
}
}
上述代码报错,原因是我使用的:
vector<T>::iterator iter;
vs2017中错误显示如下:
错误 C2679 二进制“=”: 没有找到接受“std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>”类型的右操作数的运算符(或没有可接受的转换)
错误 C2679 二进制“<<”: 没有找到接受“std::basic_string<char,std::char_traits<char>,std::allocator<char>>”类型的右操作数的运算符(或没有可接受的转换)
我试过不使用泛型而使用:
vector<int>::iterator iter;
来声明具体类型的迭代器就没有问题。
求大神解答,多谢。