Can't remove punctuation from string (compile error)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tashfeen Bhimdi

    #1

    Can't remove punctuation from string (compile error)

    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(remo ve_if(temp.begi n(), temp.end(), ispunct), temp.end());
    ----------------------------

    But I keep getting the following compilation error:

    "error: no matching function for call to
    `remove_if(__gn u_cxx::__normal _iterator<char* , std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char >,
    __gnu_cxx::__no rmal_iterator<c har*, std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char >, <unknown type>)'"

    Any idea what's wrong?

    --
    Tashfeen Bhimdi
  • Alf P. Steinbach

    #2
    Re: Can't remove punctuation from string (compile error)

    * 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(remo ve_if(temp.begi n(), temp.end(), ispunct), temp.end());
    ----------------------------
    >
    But I keep getting the following compilation error:
    >
    "error: no matching function for call to
    `remove_if(__gn u_cxx::__normal _iterator<char* , std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char >,
    __gnu_cxx::__no rmal_iterator<c har*, std::basic_stri ng<char,
    std::char_trait s<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?

    Comment

    • Victor Bazarov

      #3
      Re: Can't remove punctuation from string (compile error)

      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(remo ve_if(temp.begi n(), temp.end(), ispunct), temp.end());
      ----------------------------
      >
      But I keep getting the following compilation error:
      >
      "error: no matching function for call to
      `remove_if(__gn u_cxx::__normal _iterator<char* , std::basic_stri ng<char,
      std::char_trait s<char>, std::allocator< char >,
      __gnu_cxx::__no rmal_iterator<c har*, std::basic_stri ng<char,
      std::char_trait s<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


      Comment

      • Tashfeen Bhimdi

        #4
        Re: Can't remove punctuation from string (compile error)

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


        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, intBuildDiction ary(string &textfile) {
        map<string, intdict;
        ifstream infile;
        infile.open(tex tfile.c_str());
        string word;

        while (infile >word) {
        word.erase(remo ve_if(word.begi n(), word.end(), ispunct), word.end());
        dict[word]++;
        }

        infile.close();
        return dict;
        }
        -----------------------------


        --
        Tashfeen Bhimdi

        Comment

        • Alf P. Steinbach

          #5
          Re: Can't remove punctuation from string (compile error)

          * 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:

          >
          >
          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, intBuildDiction ary(string &textfile) {
          Just a nit (but important in general): make that 'string const&'.

          map<string, intdict;
          ifstream infile;
          infile.open(tex tfile.c_str());
          string word;
          >
          while (infile >word) {
          word.erase(remo ve_if(word.begi n(), 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?

          Comment

          • Tashfeen Bhimdi

            #6
            Re: Can't remove punctuation from string (compile error)

            >map<string, intBuildDiction ary(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

            Comment

            • Alf P. Steinbach

              #7
              Re: Can't remove punctuation from string (compile error)

              * 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?

              Comment

              Working...