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;
}
您好,當我嘗試編譯這一點,它拋出第三行錯誤:const和非const方法之間的區別?
"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? – mathematician1975 2015-03-25 08:09:31