mardi 25 mai 2021

passing const iterator as ‘this’ argument discards qualifiers while using vector::erase in a template

I am trying to remove the first value of a vector of template typenames. Using the erase() method in vector stl, I keep getting this error:

TTrie.inc:56:19: error: passing ‘const std::vector<char, std::allocator<char> >’ as ‘this’ argument discards qualifiers [-fpermissive]
   56 |     sequence.erase(it);
      |     ~~~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/10/vector:67,
                 from TTrie.h:5,
                 from TTrieTest.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1430:7: note:   in call to ‘std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = char; _Alloc = std::allocator<char>; std::vector<_Tp, _Alloc>::iterator = std::vector<char, std::allocator<char> >::iterator; std::vector<_Tp, _Alloc>::const_iterator = std::vector<char, std::allocator<char> >::const_iterator]’
 1430 |       erase(const_iterator __position)
      |       ^~~~~
In file included from TTrie.h:115,
                 from TTrieTest.cpp:1:
TTrie.inc: In instantiation of ‘TTrie<DataType>& TTrie<DataType>::operator+=(const std::vector<DataType>&) [with DataType = std::__cxx11::basic_string<char>]’:
TTrieTest.cpp:93:10:   required from here
TTrie.inc:56:19: error: passing ‘const std::vector<std::__cxx11::basic_string<char> >’ as ‘this’ argument discards qualifiers [-fpermissive]
   56 |     sequence.erase(it);
      |     ~~~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/10/vector:67,
                 from TTrie.h:5,
                 from TTrieTest.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:1430:7: note:   in call to ‘std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = std::__cxx11::basic_string<char>; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::vector<_Tp, _Alloc>::iterator = std::vector<std::__cxx11::basic_string<char> >::iterator; std::vector<_Tp, _Alloc>::const_iterator = std::vector<std::__cxx11::basic_string<char> >::const_iterator]’
 1430 |       erase(const_iterator __position)
      |       ^~~~~
In file included from TTrie.h:115,
                 from TTrieTest.cpp:1:

Is it having trouble because sequence should be const? Am I declaring the iterator wrong somehow? Here is my code. Please let me know If you have any questions:

template<typename DataType>
TTrie<DataType>& TTrie<DataType>::operator+=(const std::vector<DataType>& sequence) {
  const DataType c = sequence[0];
  const TTrie<DataType>* child = getChild(c); //Returns a pointer to a child node or a nullptr

  if (!child) {
    TTrie<DataType>* n = new TTrie<DataType>(c);
    edgeMap[c] = n;
  }

  if(sequence.size() > 1) {
    typename std::vector<DataType>::const_iterator it;
    it = sequence.cbegin(); //First value of sequence
    sequence.erase(it);
    *edgeMap[c] += sequence;
  }
  return *this;
}

Aucun commentaire:

Enregistrer un commentaire