Hello:

Was trying to assign a function pointer to a struct member and am getting compile errors. The .h file I have to use has defined the struct:

typedef void(*callback)(bool, char*, int, unsigned long, void *);

struct CallbackInfo
{
    callback func;                 
    void*    stuff;
};

What I have in my C++ file is:

static void foo(bool val, char* a, int b, unsigned long c, void * stuff)
{
...
...
}

int funXYZ ()
{
 CallbackInfo callbackInfo;
 callbackInfo.func = foo; // compiler error right here
 callbackInfo.stuff= NULL;
...
...
}

The error I get is:

value of type "void (*)(bool, char*, int, unsigned long, void *)"
          cannot be assigned to an entity of type "callback"
              > callbackInfo.func = foo;

Any help is greatly appreciated.
Thank you.

This is very strange....Using the information that you have provided here I have created a working solution; no compilation errors at all (g++ compiler). Do you return from foo?
What compiler are you using?

Member Avatar for stevee1984

This code compiles fine in Visual Studio 2005

It is a bit strange! Will it matter if the header file is wrapped around:

extern "C"

Am using GreenHills compiler (Multi). There's no return from "foo" as it has a void return type.

ok.. was pointing to an older version of the .h file which caused this problem. But thank-you very much for your time to verify that the C++ code was ok.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.