I have a problem with accessing iterator to the vector in following code (just a few important lines):

class Data 
{
...
vector<double> hodnoty;
public:
int CompareData(vector<Senzory*> data);
...
}

class Senzory : Data
{
...
const vector<double> &GetData(void){return hodnoty;}; //Returns reference to the vector hodnoty
...
}

int Data::CompareData(vector<Senzory*> data)
{
  int pocet = 0;
  vector <Data*>::iterator it,ot;
  int stop = data.size();
  int i,j,a=1e8,b, num_del;
  vector<double>::iterator it1,it2;
  
  for(i=0;i<stop;i++)
  {
    b=data.at(i)->GetData().size();
    if (b<a)
      a=b;
  }
  
  for(i=0;i<stop;i++)
  {
    it1=data.at(i)->GetData().end()-num_del;
    it2=data.at(i)->GetData().end();
    num_del = data.at(i)->GetData().size() - a;
    data.at(i)->GetData().erase( it1, it2);
  }

return stop;
}

--
int main()
{
  Senzory F60("2009_05_28_F60.txt"),F59("2009_05_28_F59.txt");
vector<Senzory*> sen_vec;
  sen_vec.push_back(&F60);
  sen_vec.push_back(&F59);

return 0;
}

What I try to do is to erase several number of elements from vector hodnoty. Following code for accessing size works:

b=data.at(i)->GetData().size();

Therefore I don't understand why following lines don't work:

it1=data.at(i)->GetData().end()-num_del;
it2=data.at(i)->GetData().end();

Errors I got are as follows:

sortier_new_v2.1.cpp: In member function ‘int Data::CompareData(std::vector<Senzory*, std::allocator<Senzory*> >)’:
sortier_new_v2.1.cpp:169: error: no match for ‘operator=’ in ‘it1 = ((const std::vector<double, std::allocator<double> >*)data.std::vector<_Tp, _Alloc>::at [with _Tp = Senzory*, _Alloc = std::allocator<Senzory*>](((long unsigned int)i))->Senzory::GetData())->std::vector<_Tp, _Alloc>::end [with _Tp = double, _Alloc = std::allocator<double>]().__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator- [with _Iterator = const double*, _Container = std::vector<double, std::allocator<double> >](((const ptrdiff_t&)((const ptrdiff_t*)(&((ptrdiff_t)num_del)))))’
/usr/include/c++/4.4/bits/stl_iterator.h:669: note: candidates are: __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >& __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >::operator=(const __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >&)
sortier_new_v2.1.cpp:170: error: no match for ‘operator=’ in ‘it2 = ((const std::vector<double, std::allocator<double> >*)data.std::vector<_Tp, _Alloc>::at [with _Tp = Senzory*, _Alloc = std::allocator<Senzory*>](((long unsigned int)i))->Senzory::GetData())->std::vector<_Tp, _Alloc>::end [with _Tp = double, _Alloc = std::allocator<double>]()’
/usr/include/c++/4.4/bits/stl_iterator.h:669: note: candidates are: __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >& __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >::operator=(const __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >&)
sortier_new_v2.1.cpp:172: error: passing ‘const std::vector<double, std::allocator<double> >’ as ‘this’ argument of ‘__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> > std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = double, _Alloc = std::allocator<double>]’ discards qualifiers

Thanks for your help in advance.

I think the trouble is due to const vector<double> &GetData() .

You are right. After I have removed const - it works fine. Thanks a lot it solved my problem. But still I don't understand why I cannot access even the iterator with: it2=data.at(i)->GetData().end(); where no modification to the vector is performed.

If you'd be allowed to do that, it would make const quite meaningless, since you'd get a non-const iterator, through which you could modify the (const) vector.

This question has already been answered. Start a new discussion instead.