rtrimws: can't compile in g++

E

eric

Dear advanced c++ programers:

I copied and tested a piece simple Trimming string code from book c+
+ cookbook in chapter4, section2
Example 4-4:Trim trailing whitespace. You can download and try it by
yourself.
 
M

Miles Bader

eric said:
--------------------
4-4.cpp: In function ‘void rtrimws(std::string&)’:
4-4.cpp:21:21: error: too many arguments to function ‘void
rtrimws(std::string&)’
4-4.cpp:20:6: note: declared here
------------

The problem seems to be that there are multiple overloaded versions of
"isspace"[*], and the compiler doesn'tn know which one you want.

Changing the function that contains line 21 to the following seems to
make it work (though I dunno if it's the best solution):

// Overloads to make cleaner calling for client code
void rtrimws(string& s) {
rtrimws (s, static_cast<int(&)(int)> (isspace));
}

[I added the cast to "int(&)(int)" to force it to choose a particular
variant of isspace...]


[*] One is the usual "int isspace(int)", but there's _also_ this:

template<typename _CharT>
bool
isspace(_CharT, const locale&);


-Miles
 
E

eric

eric said:
--------------------
4-4.cpp: In function ‘void rtrimws(std::string&)’:
4-4.cpp:21:21: error: too many arguments to function ‘void
rtrimws(std::string&)’
4-4.cpp:20:6: note: declared here
------------

The problem seems to be that there are multiple overloaded versions of
"isspace"[*], and the compiler doesn'tn know which one you want.

Changing the function that contains line 21 to the following seems to
make it work (though I dunno if it's the best solution):

   // Overloads to make cleaner calling for client code
   void rtrimws(string& s) {
     rtrimws (s, static_cast<int(&)(int)> (isspace));
   }

[I added the cast to "int(&)(int)" to force it to choose a particular
variant of isspace...]

[*] One is the usual "int isspace(int)", but there's _also_ this:

  template<typename _CharT>
    bool
    isspace(_CharT, const locale&);

-Miles

Thanks your suggestion, Eric
 

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
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top