error C2440: 'initializing' : cannot convert from 'void *' to 'void (__cdecl *)(void)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frostmur
    New Member
    • Oct 2008
    • 1

    error C2440: 'initializing' : cannot convert from 'void *' to 'void (__cdecl *)(void)

    Hi !!
    I'm trying to convert C code to C++.

    This is my function:


    static void (*selection)(vo id) = NULL;
    static void (*pick)(GLint name) = NULL;

    void zprSelectionFun c(void (*f)(void))
    {
    selection = f;
    }

    void zprPickFunc(voi d (*f)(GLint name))
    {
    pick = f;
    }


    But I have this problem when I compile:

    error C2440: 'initializing' : cannot convert from 'void *' to 'void (__cdecl *)(void)
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast


    Please!!...help me whit this problem...Thank s.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your problem is not in this code which I compiled without error using Visual Studio.NET 2008.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      (It would have helped if you had identified which line of your program was identified in the error message.)

      NULL is a data pointer. It essentially [or actually, I don't quite remember which] has type void*.

      'select' and 'pick' are function pointers. Pedantically speaking, data pointers and function pointers are not interchangeable . The C Standard allows for the possibility that a processor might use different size registers for data and function pointers.

      As it turns out, most processors and compiler implementations will happily interconvert data and function pointers. That is probably true for your compiler too. Perhaps you have the most pedantic level of error checking enabled. Or perhaps C++ is naturally pedantic about this.

      The most portable way to initialize your function pointers is to set them equal to literal zero. My long-standing practice has been to do the following:
      Code:
      #define FNULL 0L
      void (*select)(void) = FNULL;
      This is my practice for C code. There may be a preferred idiom in C++.

      Comment

      Working...