std::vectorcard;
class Information
{ ...... };
Information *Search()
{
int i=1;
string tempname;
cout<<"请输入姓名:";
cin>>tempname;
Information * temp;
temp=card.begin();
//无法从“std::_Vector_iterator<_Myvec>”转换///为“Information *”
while(i<=card.size())
{
if((temp->Get_Name())==tempname)
{ return temp; }
temp++;
i++;
}
return NULL;
}
temp=card.begin();
//无法从“std::_Vector_iterator<_Myvec>”转换///为“Information *”
请问如何很正
begin返回的是迭代器,不能直接赋值给Information指针。
下面的循环查找逻辑也错了,可以用下面一种写法
Information *temp = NULL;
std::vector<Information*>::iterator it = card.begin();
while(it < card.end())
{
temp = *it;
if((temp->Get_Name()) == tempname)
{
return temp;
}
++it;
}
return NULL
加* 引用传递数据
你的vector里面放的是不是Information指针,然后你可以通过itr来访问成员赋值等