LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   function pointer table and compile error (https://www.linuxquestions.org/questions/programming-9/function-pointer-table-and-compile-error-365367/)

dragondad 09-20-2005 05:28 PM

function pointer table and compile error
 
I have a small program which need to define a function pointer table, unfortunately, this code will be C code which need to be compiled with other C++ code (gcc on linux).
Please help me out.

I get the error like this:
gcc -c -g funct.cpp
In file included from funct.cpp:5:
funct.hpp:40: excess elements in aggregate initializer
funct.hpp:40: excess elements in aggregate initializer
funct.hpp:40: excess elements in aggregate initializer
funct.cpp: In function `int main(int, char**)':
funct.cpp:130: cannot resolve overloaded function `funcptr' based on conversion
to type `void*'


Here it is the snapshot of the code "funct.hpp"

void PrintHello1(void *params);
void PrintHello2(void *params);
void PrintHello3(void *params);

char *messages[NUM_THREADS];

struct thread_data
{
int thread_id;
int sum;
char *message;
};

struct thread_data thread_data_array[NUM_THREADS];

typedef void Func(void *);

struct PrintHi {
int msg;
Func funcptr;
};

struct PrintHi PrintHiTable[] = {
{ MSG1, PrintHello1 },
{ MSG2, PrintHello2 },
{ MSG3, PrintHello3 }
}; // funct.hpp code line 40


Another file funct.cpp

for(t=0;t<NUM_THREADS;t++) {
sum = sum + t;
thread_data_array[t].thread_id = t;
thread_data_array[t].sum = sum;
thread_data_array[t].message = messages[t];
printf("Creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, (void *)PrintHiTable[t].funcptr, (void *)&thread_data_array[t]); // funct.cpp code line 130

itsme86 09-20-2005 05:39 PM

Code:

typedef void Func(void *);
This is not a typedef for a function pointer type.

Try:
Code:

typedef return_type (*Func)(parameters);
...where return_type is the type the functions will return and parameters is the parameters list.

deiussum 09-20-2005 06:05 PM

You must have changed your functions and typedef since this thread...

Maybe you can be more clear as to why my suggestions there didn't work for you.


All times are GMT -5. The time now is 07:27 PM.