Hi, my name is Wai Chong. Right now I am trying to use pointer
to function in a C++ program. The reason why I am doing is
because I am trying to convert files generated by rpcgen
into C++ files. I have managed to convert much of the files
to C++ files except for some statements.
I have written some simple files to simulate these
conditions and these files are listed below. Please
enlighten me how can I solve this problem. I appreciate it
very much if anyone can send me a solution to this problem.
My email address is tanwc@stee.com.sg.
Thank you very much
/*******************************************/
*** test1.cpp - a C++ file
*** In this file, I am trying to assign the
*** member of struct what, ptrfn to the function
*** get_max.
*** The get_max function is written in C (in file getmax.c)
*** and compiled by a C compiler
*** This test1.cpp is compiled by a C++ compiler
/******************************************/
#include <stdio.h>
#include "test.h"
extern "C" {int get_max(int *, int);}
void main(void)
{
int var1, var2;
static int values[5] = {20,40,10,80,30};
int aa, bbaa;
what whywhy;
whywhy.ptrfn= get_max;
bbaa = (*whywhy.ptrfn)(values,5);
printf("bbaa = %d\n",bbaa);
}
/*******************************************/
*** getmax.c - a C file
*** This function returns the maximum value
*** from a array of integers
/******************************************/
#include "test.h"
get_max(value, size)
int *value;
int size;
{
int i =1;
int max;
max = *value++;
while(i++<size)
{
if(max < *value)
max = *value;
value++;
}
printf("What value\n");
return(max);
}
/*******************************************/
*** test.h - a header file
*** This header file is included in both
*** test1.cpp and getmax.c files
/******************************************/
typedef struct what{
int a;
int (*ptrfn)();
};
/*******************************************/
*** this is the error message generated
*** by the C++ compiler
*** All these source files are compiled
*** in HP-UNIX environment
/******************************************/
>>: make -f maketest
CC -c test1.cpp
CC: "test1.cpp", line 27: error: bad assignment type: int (*)() = int
(*)(int *, int ) (1213)
CC: "test1.cpp", line 28: error: unexpected 1 argument for ? (1265)
*** Error code 1
|