【发布时间】:2015-05-28 19:24:41
【问题描述】:
int CRegister::CountCars(const string& name, const string& surname)const{
const pair<string,string> wholename(name,surname);
vector<CDriver>::iterator Diterator=lower_bound(m_Drivers.begin(),m_Drivers.end(),wholename);
if (Diterator<m_Drivers.end()){
if(Diterator->m_name.compare(wholename.first)!=0 || Diterator->m_surname.compare(wholename.second)!=0) return 0;
return Diterator->m_DriversNumber;
}
return 0;
}
你好,当我尝试编译这个时,它在第三行抛出错误:
"conversion from ‘__gnu_cxx::__normal_iterator<const CDriver*, std::vector<CDriver> >’ to non-scalar type ‘std::vector<CDriver>::iterator {aka __gnu_cxx::__normal_iterator<CDriver*, std::vector<CDriver> >}’ requested
当我将函数 CountCars 设置为非常量时,它可以毫无问题地编译。我应该改变什么来解决这个问题? (函数必须是 const)
【问题讨论】:
-
你试过使用 const_iterator 吗?
标签: c++ vector type-conversion constants