C++迭代器——奇怪的转换
C++ iterator - strange conversion
我在使用迭代器时遇到了非常奇怪的错误,我不确定该怎么想:error: could not convert ‘0’ from ‘int’ to ‘CCarList’
CCarList CRegister::ListCars(const string& name, const string& surname) const {
auto ownerIt = findOwner(name, surname);
if (ownerIt == m_owners.end()) return 0; //Error is on this line
//...
}
查找所有者:
vector<TOwner*>::const_iterator CRegister::findOwner(const string& name,
const string& surname) const
{
return lower_bound(m_owners.begin(), m_owners.end(), TOwner(name, surname),
[](const TOwner* a, const TOwner& b){return (*a) < b;});
}
m_owners
是 vector<TOwner*>
(TOwner 是结构体。)
CCarList:
class CCarList {
vector<TCar*>::iterator m_it;
const vector<TCar*>::iterator m_end;
public:
CCarList(vector<TCar*>::iterator it, const vector<TCar*>::iterator end):
m_it(it), m_end(end){}
CCarList(){}
string RZ() const;
bool AtEnd() const;
void Next();
};
ListCars
return一个CCarList
。你试试return0,一个int
。没有从 int
到 CCarList
的转换,因此尝试失败。
我在使用迭代器时遇到了非常奇怪的错误,我不确定该怎么想:error: could not convert ‘0’ from ‘int’ to ‘CCarList’
CCarList CRegister::ListCars(const string& name, const string& surname) const {
auto ownerIt = findOwner(name, surname);
if (ownerIt == m_owners.end()) return 0; //Error is on this line
//...
}
查找所有者:
vector<TOwner*>::const_iterator CRegister::findOwner(const string& name,
const string& surname) const
{
return lower_bound(m_owners.begin(), m_owners.end(), TOwner(name, surname),
[](const TOwner* a, const TOwner& b){return (*a) < b;});
}
m_owners
是 vector<TOwner*>
(TOwner 是结构体。)
CCarList:
class CCarList {
vector<TCar*>::iterator m_it;
const vector<TCar*>::iterator m_end;
public:
CCarList(vector<TCar*>::iterator it, const vector<TCar*>::iterator end):
m_it(it), m_end(end){}
CCarList(){}
string RZ() const;
bool AtEnd() const;
void Next();
};
ListCars
return一个CCarList
。你试试return0,一个int
。没有从 int
到 CCarList
的转换,因此尝试失败。