tags:

views:

67

answers:

2

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 ==========
+6  A: 

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

Scharron
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.
David Thornley
A: 

Change

isdigit myobject; 

to

struct isdigit myobject;
DumbCoder
Unless we are using C (which we are not) then this is not required. Soon to become a -1 downvote.
Martin York
@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
@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.
Martin York