operator functions
according to this site
http://www.cplusplus.com/reference/std/functional/unary_function/
this code should work
#include <iostream>
#include <functional>
us开发者_StackOverflowing 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;
}
but it shows that somewhere bracket is missed but where?
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 ==========
isdigit is an already existing function. Try renaming your class IsDigit for example, or put it in a namespace, and it should work.
Change
isdigit myobject;
to
struct isdigit myobject;
精彩评论