error: cannot convert 'std::basic_string<char>::iterator ...' to 'const char* for argument '1' ...'

anonymous picture anonymous · Dec 18, 2015 · Viewed 11.1k times · Source

I'm getting the following error:

error: cannot convert 'std::basic_string<char>::iterator {aka __gnu_cxx::__normal
_iterator<char*, std::basic_string<char> >}' to 'const char*' for argument '1' 
to 'int remove(const char*)'

For some reason, my program compiles perfectly when I'm working on a Mac... but once I use a Linux machine, this error pops up in more than one place.

Here's one of the instances where the error pops up:

SomeClass::SomeClass(string t, string art, Time dur) {
    char chars[] = ",";
    t.erase(std::remove(t.begin(), t.end(), chars[0]), t.end());
    art.erase(std::remove(art.begin(), art.end(), chars[0]), art.end());
    // Some more code ...
}

More specifically, the error is coming from this line:

t.erase(std::remove(t.begin(), t.end(), chars[0]), t.end());

Does anyone know how to approach this problem?

Answer

LogicStuff picture LogicStuff · Dec 18, 2015

You forgot to #include <algorithm>, where std::remove is located. Without that, your compiler only knows about this std::remove (I get the same error with Visual C++ 14), which is defined in indirectly included <cstdio> header.

Different behavior among compilers is a result of different #include hierarchies of the standard library implementations.