0

according to this site

http://www.cplusplus.com/reference/std/functional/unary_function/ 

this code should work

#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; } 

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 ========== 
5
  • The error message would tell you exactly where. Commented Jul 22, 2010 at 16:20
  • What does the compiler error say? Commented Jul 22, 2010 at 16:20
  • The error message you posted doesn't look like it matches the code... But Scharron's answer seems as correct as any. Commented Jul 22, 2010 at 16:27
  • Just note your function should be marked const. Commented Jul 22, 2010 at 16:28
  • You should turn on the keyword coloring. Or customize it yourself. Commented Jul 22, 2010 at 16:59

2 Answers 2

6

isdigit is an already existing function. Try renaming your class IsDigit for example, or put it in a namespace, and it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

What's worse is that, depending on compiler and previous includes, it might be a previously existing macro. I've used systems that implemented <ctypes.h> as macros, although presumably there were also functions should one want them.
0

Change

isdigit myobject; 

to

struct isdigit myobject; 

3 Comments

Unless we are using C (which we are not) then this is not required. Soon to become a -1 downvote.
@Martin: in fact this is correct, according to 3.4.4: "An elaborated-type-specifier may be used to refer to a previously declared class-name or enum-name even though the name has been hidden by a non-type declaration". struct isdigit is an elaborated-type-specifier.
@Mike Seymour: Never said it was not correct! But to make code easier to maintain you should stick to the normal way of doing stuff (and not look for these odd corners that are correct but largely unused). This style is allowed because it is there to make C++ backward compatible with C at the source level (thus this is a C construct that is not required by C++). Also when used in the context of the original question does not solve the problem so the answer is incorrect as well.