Reputation: 1
The erase function is giving an error of invalid arguments but I have passed
1st argument = index no.
2nd argument = no. of characters
then also it is giving error. Eg
word3 = word2.erase(word.begin(), word.length()/2-1);
Upvotes: 0
Views: 562
Reputation: 310940
Member function begin()
of class std::string
returns iterator. That is its return type either std::string::iterator
or std::string::const_iterator
.
You are trying to use member function erase
that has parameters of type std::string::size_type
:
basic_string& erase(size_type pos = 0, size_type n = npos);
If you want to use this member function you should write the call like
word2.erase( 0, word.length()/2 - 1 )
If you want to use the function that uses iterators that is the following member function
iterator erase(const_iterator first, const_iterator last);
then the call can look like
word2.erase( word.begin(), std::next( word.begin(), word.length()/2 - 1 ) )
or simply
word2.erase( word.begin(), word.begin() + word.length()/2 - 1 )
I hope that is not a typo that you call the function for object with name word2
and in the expression used as the argument you use object with name word
Upvotes: 0
Reputation: 56547
Looking at http://en.cppreference.com/w/cpp/string/basic_string/erase (1) you see that you need to pass the index of the first character to erase and the number of how many characters to erase. You pass an iterator instead as the first argument. Just do
word3 = word2.erase(0, word.length()/2 -1);
// ^^^^
// this should probably be word2
or use the (3) overload that accepts a range:
word3 = word2.erase(word2.begin(), std::next(word2.begin(), word2.length()/2 -1));
I also believe you should have word2
inside your erase
, not word
.
Upvotes: 2