我的程序在vs2010上没有显示错误,为什么编译会出错呢?
我的代码如下:#include<iostream>#include<string>#include<vector>usingnamespacestd;template<t...
我的代码如下:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template <typename init,typename it>
init findmind(init first,init last,const it& val)
{
while(first != last)
{ if (*first = val)
{return first;}
++first;
}
return last;
}
int main(){
int ai[]={1,2,3,4,5,6,7};
string bi[]={"this","is","a","good","chance"};
vector<int> vec(ai,ai+7);
vector<string> voc(bi,bi+5);
vector<int>::iterator it=vec.begin();
it = findmind(vec.begin(),vec.end(),6);
if(it!=vec.end())
{cout<<"find the element."<<*it<<endl;}
else{cout<<"this element not find."<<endl;}
vector<string>::iterator itr;
itr = findmind(voc.begin(),voc.end(),"chance");
if(itr!=voc.end())
{cout<<"find the element."<<*itr<<endl;}
else{cout<<"this element not find."<<endl;}
return 0;
}
问题报错:
1>c:\users\hudai\documents\visual studio 2010\projects\myapp\myapp\myapp.cpp(9): error C2451: “std::basic_string<_Elem,_Traits,_Ax>”类型的条件表达式是非法的
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1> c:\users\hudai\documents\visual studio 2010\projects\myapp\myapp\myapp.cpp(28): 参见对正在编译的函数 模板 实例化“init findmind<std::_Vector_iterator<_Myvec>,const char[7]>(init,init,it (&))”的引用
1> with
1> [
1> init=std::_Vector_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
1> _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>,
1> it=const char [7]
1> ]
1>生成失败。 展开
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template <typename init,typename it>
init findmind(init first,init last,const it& val)
{
while(first != last)
{ if (*first = val)
{return first;}
++first;
}
return last;
}
int main(){
int ai[]={1,2,3,4,5,6,7};
string bi[]={"this","is","a","good","chance"};
vector<int> vec(ai,ai+7);
vector<string> voc(bi,bi+5);
vector<int>::iterator it=vec.begin();
it = findmind(vec.begin(),vec.end(),6);
if(it!=vec.end())
{cout<<"find the element."<<*it<<endl;}
else{cout<<"this element not find."<<endl;}
vector<string>::iterator itr;
itr = findmind(voc.begin(),voc.end(),"chance");
if(itr!=voc.end())
{cout<<"find the element."<<*itr<<endl;}
else{cout<<"this element not find."<<endl;}
return 0;
}
问题报错:
1>c:\users\hudai\documents\visual studio 2010\projects\myapp\myapp\myapp.cpp(9): error C2451: “std::basic_string<_Elem,_Traits,_Ax>”类型的条件表达式是非法的
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1> c:\users\hudai\documents\visual studio 2010\projects\myapp\myapp\myapp.cpp(28): 参见对正在编译的函数 模板 实例化“init findmind<std::_Vector_iterator<_Myvec>,const char[7]>(init,init,it (&))”的引用
1> with
1> [
1> init=std::_Vector_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
1> _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>,
1> it=const char [7]
1> ]
1>生成失败。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询