LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2005, 05:28 PM   #1
dragondad
LQ Newbie
 
Registered: Aug 2005
Posts: 18

Rep: Reputation: 0
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
 
Old 09-20-2005, 05:39 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 09-20-2005, 06:05 PM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
(I know I am inept) what is a pointer function in C ? cigarstub Programming 3 09-27-2005 05:06 PM
question about function pointer compile error dragondad Programming 5 09-19-2005 11:42 AM
Function Pointer as an Argument in C trutnev Programming 5 05-24-2005 10:22 AM
void * pointer in function xailer Programming 23 01-16-2004 02:14 PM
c++ Pointer to Member Function Wondre Programming 0 02-15-2003 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration