vector<T>::iterator iter
这是一个打印容器里元素的程序,为了使函数有普适性,用了模板,但是出了错,看了好久也没明白,求指点!#include<vector>#include<iostream>us...
这是一个打印容器里元素的程序,为了使函数有普适性,用了模板,但是出了错,看了好久也没明白,求指点!
#include<vector>
#include<iostream>
using namespace std;
template <typename T>
void printvector(vector<T>& v,char*s)
{
if(v.empty())
cout<<s<<"是空的"<<endl;
else
{
vector<T>::iterator iter=v.begin();
cout<<s<<"为 ";
for(;iter!=v.end();iter++)
cout<<*iter<<" ";
cout<<endl;
}
}
int main()
{
vector<int> g(5,9);
printvector(g,"g");
return 0;
}
编译器说In function 'void printvector(std::vector<T>&, char*)': need 'typename' before 'std::vector<T>::iterator' because 'std::vector<T>' is a dependent scope|
为什么会这样? 展开
#include<vector>
#include<iostream>
using namespace std;
template <typename T>
void printvector(vector<T>& v,char*s)
{
if(v.empty())
cout<<s<<"是空的"<<endl;
else
{
vector<T>::iterator iter=v.begin();
cout<<s<<"为 ";
for(;iter!=v.end();iter++)
cout<<*iter<<" ";
cout<<endl;
}
}
int main()
{
vector<int> g(5,9);
printvector(g,"g");
return 0;
}
编译器说In function 'void printvector(std::vector<T>&, char*)': need 'typename' before 'std::vector<T>::iterator' because 'std::vector<T>' is a dependent scope|
为什么会这样? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询