how to pass a variable to a function defined with no variables in C++

Z

zhybear

I try add some functions like sqrt(), exp() to my structure, and then
perform the compuation of these functions

Here is what i did

typedef struct symtab {
char *name;
double value;
double (*funcptr)();
} symtab;

void addfunc(char *name, double (*func)())
{
symtab *sp = look(name);
sp->funcptr = func;
}
/* symlook is a routine to search if name is already in the table */

main()
{
extern double sqrt(), exp(), log();

addfunc("sqrt", sqrt);
addfunc("exp", exp);
addfunc("log", log);
yyparse();
}
Then I need to perform something like
double k;
(sp->funcptr)(k)

However, g++ complier doesn't like me to do this since it said there
are too many arguments for funcptr (it may be sqrt or exp). How to
avoid this issue?
 
A

Alf P. Steinbach

* zhybear:
However, g++ complier doesn't like me to do this since it said there
are too many arguments for funcptr (it may be sqrt or exp). How to
avoid this issue?

Have you thought about adding the formal arguments to the declarations?
 
L

Lionel B

zhybear said:
I try add some functions like sqrt(), exp() to my structure, and then
perform the compuation of these functions

Here is what i did

typedef struct symtab {
char *name;
double value;
double (*funcptr)();

Seeing as sqrt() and exp() take a single "double" argument, surely you want:

double (*funcptr)(double);

and adjust the rest of your code accordingly.
 
K

Karl Heinz Buchegger

zhybear said:
Then I need to perform something like
double k;
(sp->funcptr)(k)

However, g++ complier doesn't like me to do this since it said there
are too many arguments for funcptr (it may be sqrt or exp). How to
avoid this issue?

There are 2 possibilities:
1. Either you program in C, then use a C compiler
2. Or you program in C++, then you need to be aware of the
small changes that were made from C to C++
At the moment you program in C but use a C++ compiler.

Ad 2.)
By declaring the function pointer correctly.
All your functions take a double and return a double.
Thus you should say so:

struct symtab { // there is no need for the typedef, this is C++ !
char * name; // prefer std::string over character arrays
double value;
double (*funcptr)( double );
};

Same for the parameter list of addfunc.
To save yourself from simple typing errors. It is always a good idea
to use a typedef when you work with function pointers (That is a typedef
you should have made!)

typedef double (*DblFnctDbl) (double); // DblFnctDbl is a function pointer
// to a function which takes a double
// and returns a double

struct symtab { // there is no need for the typedef, this is C++ !
char * name; // prefer std::string over character arrays
double value;
DblFnctDbl funcptr;
};

void addfunc( char* name, DblFnctDbl funct )
{
symtab *sp = look(name);
sp->funcptr = func;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top