Can't remove punctuation from string (compile error)

T

Tashfeen Bhimdi

I'm trying to remove punctuation from a string with the following
code:

----------------------------
#include <string>
#include <algorithm>
#include <cctype>
..
using namespace std
..
..
..
string temp = "blah?yo!";
temp.erase(remove_if(temp.begin(), temp.end(), ispunct), temp.end());
----------------------------

But I keep getting the following compilation error:

"error: no matching function for call to
`remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, <unknown type>)'"

Any idea what's wrong?
 
A

Alf P. Steinbach

* Tashfeen Bhimdi:
I'm trying to remove punctuation from a string with the following
code:

----------------------------
#include <string>
#include <algorithm>
#include <cctype>
.
using namespace std
.
.
.
string temp = "blah?yo!";
temp.erase(remove_if(temp.begin(), temp.end(), ispunct), temp.end());
----------------------------

But I keep getting the following compilation error:

"error: no matching function for call to
`remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, <unknown type>)'"

Any idea what's wrong?

'unknown type' means the compiler doesn't know what that thing is.

Which means you've forgotten to include the relevant header (I'll leave
it to you to find out which header that is).

Also, there's a missing semicolon; always copy and paste code, otherwise
the code presented may have other errors than your real code, and may
not exemplify the real code at all.
 
V

Victor Bazarov

Tashfeen said:
I'm trying to remove punctuation from a string with the following
code:

----------------------------
#include <string>
#include <algorithm>
#include <cctype>
.
using namespace std
.
.
.
string temp = "blah?yo!";
temp.erase(remove_if(temp.begin(), temp.end(), ispunct), temp.end());
----------------------------

But I keep getting the following compilation error:

"error: no matching function for call to
`remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, <unknown type>)'"

Any idea what's wrong?

'ispunct' takes 'int' as its argument, not 'char'. Could that be
the cause?

V
 
T

Tashfeen Bhimdi

'unknown type' means the compiler doesn't know what that thing is.
Which means you've forgotten to include the relevant header (I'll leave
it to you to find out which header that is).

Also, there's a missing semicolon; always copy and paste code, otherwise
the code presented may have other errors than your real code, and may
not exemplify the real code at all.


I believe I have all the headers correct (string, algorithm for
remove_if, and cctype for ispunct). The missing semicolon was a typo on
my part.

I was just following what I read in this post:
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/50b4b7b2b54e6015/01c8495abc35fd6b

Seemed simple enough. Here is the function I'm using the code in,
copy&paste:

-----------------------------
#include <iostream>
#include <map>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

..
..
..

map<string, int> BuildDictionary(string &textfile) {
map<string, int> dict;
ifstream infile;
infile.open(textfile.c_str());
string word;

while (infile >> word) {
word.erase(remove_if(word.begin(), word.end(), ispunct), word.end());
dict[word]++;
}

infile.close();
return dict;
}
 
A

Alf P. Steinbach

* Tashfeen Bhimdi:
I believe I have all the headers correct (string, algorithm for
remove_if, and cctype for ispunct).

Yes, on reflection, you do. Sorry that I leapt to an unwarranted
conclusion.

The missing semicolon was a typo on
my part.

I was just following what I read in this post:
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/50b4b7b2b54e6015/01c8495abc35fd6b


Seemed simple enough. Here is the function I'm using the code in,
copy&paste:

-----------------------------
#include <iostream>
#include <map>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

.
.
.

map<string, int> BuildDictionary(string &textfile) {

Just a nit (but important in general): make that 'string const&'.

map<string, int> dict;
ifstream infile;
infile.open(textfile.c_str());
string word;

while (infile >> word) {
word.erase(remove_if(word.begin(), word.end(), ispunct),
word.end());
dict[word]++;
}

infile.close();
return dict;
}

Try

static_cast<int(*)(int)>( ispunct )

to disambiguate which function 'ispunct' refers to.

The problem seems to be that one of the headers you do include in turn
includes a definition of the C++ library's own 'ispunct', from <locale>,
in addition to the C library's 'ispunct' from <cctype>.

Hth.,

- Alf
 
T

Tashfeen Bhimdi

map said:
Just a nit (but important in general): make that 'string const&'.

Done, thanks for the tip.
Try

static_cast<int(*)(int)>( ispunct )

to disambiguate which function 'ispunct' refers to.

The problem seems to be that one of the headers you do include in turn
includes a definition of the C++ library's own 'ispunct', from <locale>,
in addition to the C library's 'ispunct' from <cctype>.


Perfect, that line works!

So how did you know what the problem was? I couldn't figure out from
the error and Eclipse was no help at all either (what I'm currently
using to code).

And another question, why does that static_cast<....> work to
disambiguate? Strange how it specifies which library to use.

Thanks again.
 
A

Alf P. Steinbach

* Tashfeen Bhimdi:
* Alf P. Steinbach:


Perfect, that line works!

So how did you know what the problem was?

'unknown type' means the compiler doesn't know what that thing is. In
my first posting I (erronously) assumed that was because the relevant
header hadn't been included. Another likely possibility, which it seems
applied here, is that there is an ambigious reference to something.

And another question, why does that static_cast<....> work to
disambiguate? Strange how it specifies which library to use.

It doesn't specify the library: it specifies the function signature,
i.e. which function overload.

As an alternative you could define your own wrapper function, e.g.

bool isPunctuation( char ch ) { return !!ispunct( ch ); }

and use that directly without any disambiguation device, since now there
would be no ambiguity.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top