-iostream-
2018-03-17 06:45C++模板中使用STL迭代器的问题:vector<T>::iterator声明报错的原因及解决方法
10#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;
来声明具体类型的迭代器就没有问题。
求大神解答,多谢。
- 点赞
- 回答
- 收藏
- 复制链接分享
2条回答
为你推荐
- C++模板中使用STL迭代器的问题:vector<T>::iterator声明报错的原因及解决方法
- stl
- iterator
- 迭代器
- c++
- 2个回答