26

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?

pfnuesel
  • 14,093
  • 14
  • 58
  • 71
anonymous
  • 815
  • 3
  • 13
  • 21
  • It is working [here](https://ideone.com/jZh5x5). Can you produce an [mcve]? – NathanOliver Dec 18 '15 at 18:24
  • 3
    Its not picking up the right remove function, did you include ? (Sadly theres another remove in ) – Borgleader Dec 18 '15 at 18:25
  • Read the compiler error properly. You missed out the important part in the question title. It tells you it is trying to call `int remove(const char*)` which is clearly not the function you want to call. – Jonathan Wakely Dec 18 '15 at 19:00

1 Answers1

43

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.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74