5

I am stripping off double quotes from a string, but I keep getting this error from the following function. What is the problem here?

void readCSVCell(stringstream& lineStream, string& s) {
    std::getline(lineStream,s,',');
    s.erase(remove( s.begin(), s.end(), '\"' ), s.end());
}

[ERROR]

c.cpp: In function void readCSVCell(std::stringstream&, std::string&):
c.cpp:11: error: cannot convert __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > to const char* for argument 1 to int remove(const char*)

ildjarn
  • 62,044
  • 9
  • 127
  • 211
user236215
  • 7,278
  • 23
  • 59
  • 87

6 Answers6

14

Don't you want something like:

s.erase(remove( s.begin(), s.end(), '\"' ),s.end());

As remove returns "A forward iterator pointing to the new end of the sequence, which now includes all the elements with a value other than value" rather than removing the values.

It compiles fine for me though (with gcc 4.4), so perhaps you just need to include <algorithm> and make sure you are either using namespace std or qualify the name.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • It compiles fine for me, so I guess the error is with the namespacing of remove or a conflict with another header file as Space_C0wb0y suggests. – Jeff Foster Apr 15 '11 at 08:55
4

Do you have stdio.h included? Then there could be a conflict with remove. This is the reason why you always should prefix std-calls with, well, std::.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
3

Use std::remove not remove

Erik
  • 88,732
  • 13
  • 198
  • 189
2

You can use below code to remove double quotes from string in C++.

stringVariable.erase(
    std::remove(stringVariable.begin(), stringVariable.end(), '\"'), 
    stringVariable.end());
Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51
1

remove is algorithm, hence you need to do #include <algorithm>. Then while using you should use it as std::remove(...).

Naveen
  • 74,600
  • 47
  • 176
  • 233
1

remove requires the algorithm header and is from std namespace

I do find the C++ Reference very helpful for quickly getting usage examples and what headers are required. It may not have complete information for some things but it helps as a good start if I am not sure about how to use some parts of C Library, stream Library, strings library, STL Containers or STL Algorithms

dubnde
  • 4,359
  • 9
  • 43
  • 63