根据这个网站
http://www.cplusplus.com/reference/std/functional/unary_function/
此代码应该可以使用
#include <iostream>
#include <functional>
using namespace std;
struct isdigit : public unary_function<char,bool>{
bool operator() (char a){ return (a>='0' && a<='9');}
};
int main(){
isdigit myobject;
isdigit::argument_type input;
isdigit::result_type result;
cout<<"please enter char";
cin>>input;
result=myobject(input);
cout<<"char"<<input<<"is "<<(result?"digit":"word")<<"\n";
return 0;
}
但它显示错过某处括号但在哪里?
Configuration: Debug Win32 ------
1> function_in_c++.cpp
1>c:\users\david\documents\visual studio 2010\projects\functions_in_c++\function_in_c++.cpp(12): error C2146: syntax error : missing ';' before identifier 'object'
1>c:\users\david\documents\visual studio 2010\projects\functions_in_c++\function_in_c++.cpp(12): warning C4551: function call missing argument list
1>c:\users\david\documents\visual studio 2010\projects\functions_in_c++\function_in_c++.cpp(12): error C2065: 'object' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\functions_in_c++\function_in_c++.cpp(17): error C3861: 'object': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
答案 0 :(得分:6)
isdigit是一个已经存在的函数。 尝试重命名您的类IsDigit,或者将其放在命名空间中,它应该可以工作。
答案 1 :(得分:0)
更改
isdigit myobject;
到
struct isdigit myobject;