Well in my program i have made a class of name ‘Transition’.
When i made a vector of Transition type named delta_.
And when later in one of my functions implementation of the class where i have declared delta_, i tried to use iterator with the syntax given below:
vector<Transition>::iterator it;
it=this->delta_.begin();
i got these two errors:
In constructor `__gnu_cxx::__normal_iterator<_Iterator, Container>::_normal_iterator(const __gnu_cxx::__normal_iterator<_Iter, _Container>&) [with _Iter = const Fa::Transition*, _Iterator = Fa::Transition*, _Container = std::vector >]’:
invalid conversion from
const Fa::Transition* const' toFa::Transition*’
Now i really have no idea where the mistake is. Can anybody please help!!
Let me guess, are you doing
it=this->delta_.begin();in aconstmethod of the class, anddelta_is a member of the class?A constness of the method guarantees that members of the class will not changed. But the variable
ithas non-const typeiterator, and that gives possibility to change the memberdelta_which makes the protection corrupt.The
std::vectorhas two overloaded methodsbegin()If you use
begin()in a method which isconst, compiler calls second one.So you need to refuse from constness of the method, or use
const_iterator.Another possible way, which I do not recommend because of blurring constness of the object, is to put your vector on a heap and operate with a pointer to it.