Find the answer to your Linux question:
Results 1 to 2 of 2
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    19

    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

  2. #2
    Linux Newbie
    Join Date
    Nov 2004
    Location
    New York
    Posts
    150
    Shouldn't "typedef void Func(void *);" be "typedef void (*Func)(void *);"? What's the signature for pthread_create, does it really take void*?
    \"Nifty News Fifty: When news breaks, we give you the pieces.\" - Sluggy Freelance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •