Can't remove punctuation from string (compile error)

Discussion in 'C++' started by Tashfeen Bhimdi, Oct 11, 2006.

  1. 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?

    --
    Tashfeen Bhimdi
     
    Tashfeen Bhimdi, Oct 11, 2006
    #1
    1. Advertising

  2. * 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.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 11, 2006
    #2
    1. Advertising

  3. Tashfeen Bhimdi wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Oct 11, 2006
    #3
  4. > '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;
    }
    -----------------------------


    --
    Tashfeen Bhimdi
     
    Tashfeen Bhimdi, Oct 11, 2006
    #4
  5. * 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).


    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


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 11, 2006
    #5
  6. >> map<string, int> BuildDictionary(string &textfile) {
    >
    > 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.

    --
    Tashfeen Bhimdi
     
    Tashfeen Bhimdi, Oct 11, 2006
    #6
  7. * Tashfeen Bhimdi:
    > * Alf P. Steinbach:
    >>
    >> 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?


    '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.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 11, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dfhLASST

    Remove punctuation from String?

    dfhLASST, Nov 11, 2004, in forum: Java
    Replies:
    4
    Views:
    55,564
    Woebegone
    Nov 11, 2004
  2. kylin
    Replies:
    1
    Views:
    569
    Chris Rebert
    Nov 4, 2009
  3. Beznas
    Replies:
    8
    Views:
    217
    Evertjan.
    Sep 10, 2003
  4. Kev Jackson
    Replies:
    12
    Views:
    187
    Adam Sanderson
    Jan 12, 2006
  5. Anat
    Replies:
    2
    Views:
    238
    Lasse Reichstein Nielsen
    May 25, 2006
Loading...

Share This Page