【发布时间】:2010-09-09 19:16:19
【问题描述】:
我正在关注 O'Reilly 的 C++ Cookbook 一书,我尝试了其中一个示例,代码如下:
#include <string>
#include <iostream>
#include <cctype>
#include <cwctype>
using namespace std;
template<typename T, typename F>
void rtrimws(basic_string<T>& s, F f){
if(s.empty())
return;
typename basic_string<T>::iterator p;
for(p = s.end(); p != s.begin() && f(*--p););
if(!f(*p))
p++;
s.erase(p, s.end());
}
void rtrimws(string& ws){
rtrimws(ws, isspace);
}
void rtrimws(wstring& ws){
rtrimws(ws, iswspace);
}
int main(){
string s = "zing ";
wstring ws = L"zonh ";
rtrimws(s);
rtrimws(ws);
cout << s << "|\n";
wcout << ws << "|\n";
}
当我尝试编译它时,我收到以下错误
trim.cpp: In function ‘void rtrimws(std::string&)’:
trim.cpp:22: error: too many arguments to function ‘void rtrimws(std::string&)’
trim.cpp:23: error: at this point in file
我不明白出了什么问题。如果我不使用 char 版本(字符串)而只使用 wchar_t 版本,一切运行顺利。
顺便说一句,我在 64 位 ubuntu 机器上使用 g++ 4.4.3
【问题讨论】:
-
顺便说一句,它在 VC++ 上编译得很好
-
在关闭这个帖子之前,为了进一步学习,有一些事情我想讨论一下。该代码当然可以在 VC++ 中编译,我尝试过没有任何问题,但在 GCC 中没有。函数 isspace 在 cctypes.h 中声明为 __exctype (isspace);这真的是 extern int isspace(int) throw ()