Conversion from 'void*' to pointer to non-'void' requires an explicit cast

Discussion in 'C++' started by Hakirato, Oct 4, 2006.

  1. Hakirato

    Hakirato Guest

    Hi

    I have this compilation problem when trying to convert C code to C++.

    My function looks like this:

    void FTPServer_ThreadLoop( void *arg_p )
    {

    FTPSettings_t* FTPSettings_p;

    FTPSettings_p = arg_p;
    ...
    ...
    ...

    }

    typedef struct
    {
    FTPState_t State;
    SOCKADDR_STORAGE sa;
    SOCKADDRLEN salen;
    SOCKET sock;
    } FTPSettings_t;


    When I compile I have this problem:

    error C2440: '=' : cannot convert from 'void *' to 'FTPSettings_t *'
    Conversion from 'void*' to pointer to non-'void' requires an
    explicit cast


    Please help me how to solve this problem. Thanks
    Hakirato, Oct 4, 2006
    #1
    1. Advertising

  2. Hakirato

    Howard Guest

    "Hakirato" <> wrote in message
    news:...
    > Hi
    >
    > I have this compilation problem when trying to convert C code to C++.
    >
    > My function looks like this:
    >
    > void FTPServer_ThreadLoop( void *arg_p )
    > {
    >
    > FTPSettings_t* FTPSettings_p;
    >
    > FTPSettings_p = arg_p;
    > ..
    > ..
    > ..
    >
    > }
    >
    > typedef struct
    > {
    > FTPState_t State;
    > SOCKADDR_STORAGE sa;
    > SOCKADDRLEN salen;
    > SOCKET sock;
    > } FTPSettings_t;
    >
    >
    > When I compile I have this problem:
    >
    > error C2440: '=' : cannot convert from 'void *' to 'FTPSettings_t *'
    > Conversion from 'void*' to pointer to non-'void' requires an
    > explicit cast
    >
    >
    > Please help me how to solve this problem. Thanks
    >


    Do exactly what it says: add an explicit cast (and might as well make it a
    one-liner while you're at it):

    FTPSettings_t* FTPSettings_p = (FTPSettings_t*)arg_p;

    or

    FTPSettings_t* FTPSettings_p = static_cast<FTPSettings_t*>(arg_p);

    -Howard
    Howard, Oct 4, 2006
    #2
    1. Advertising

  3. Re: Conversion from 'void*' to pointer to non-'void' requires anexplicit cast

    * Hakirato:
    > Hi
    >
    > I have this compilation problem when trying to convert C code to C++.
    >
    > My function looks like this:
    >
    > void FTPServer_ThreadLoop( void *arg_p )
    > {
    >
    > FTPSettings_t* FTPSettings_p;
    >
    > FTPSettings_p = arg_p;
    > ..
    > ..
    > ..
    >
    > }
    >
    > typedef struct
    > {
    > FTPState_t State;
    > SOCKADDR_STORAGE sa;
    > SOCKADDRLEN salen;
    > SOCKET sock;
    > } FTPSettings_t;
    >
    >
    > When I compile I have this problem:
    >
    > error C2440: '=' : cannot convert from 'void *' to 'FTPSettings_t *'
    > Conversion from 'void*' to pointer to non-'void' requires an
    > explicit cast
    >
    >
    > Please help me how to solve this problem. Thanks


    One purely technical answer is:

    void FTPServer_ThreadLoop_C( void* pSettings )
    {
    FTPServer_ThreadLoop( *static_cast<FTPSettings*>( pSettings ) );
    }

    which calls the following more type safe function:

    void FTPServer_ThreadLoop( FTPSettings& settings )
    {
    ...
    }

    Given that, for maintainability it can be a good idea to do something like

    struct FTPState
    {
    FTPState_t State;
    SOCKADDR_STORAGE sa;
    SOCKADDRLEN salen;
    SOCKET sock;

    void ThreadLoop()
    {
    ...
    }
    };

    namespace g
    {
    FTPState* pFtpState; // Managed by code not shown here.
    }

    void FTPServer_ThreadLoop( FTPSettings& settings )
    {
    g::pFtpState->ThreadLoop();
    }

    and then notice that there's probably initialization (which should be a
    constructor), destruction (which should be a destructor), and matters of
    access to be granted or restricted, and so on.

    Actually I wouldn't use the namespace as shown above, and in fact the
    code wouldn't resemble this at all. <g> I'm trying to write something
    you'd find at least slightly familiar. Hope this helps.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 4, 2006
    #3
  4. Hakirato

    Hakirato Guest

    Hi

    Thanks for helping me this out..

    //Hakirato

    Alf P. Steinbach skrev:

    > * Hakirato:
    > > Hi
    > >
    > > I have this compilation problem when trying to convert C code to C++.
    > >
    > > My function looks like this:
    > >
    > > void FTPServer_ThreadLoop( void *arg_p )
    > > {
    > >
    > > FTPSettings_t* FTPSettings_p;
    > >
    > > FTPSettings_p = arg_p;
    > > ..
    > > ..
    > > ..
    > >
    > > }
    > >
    > > typedef struct
    > > {
    > > FTPState_t State;
    > > SOCKADDR_STORAGE sa;
    > > SOCKADDRLEN salen;
    > > SOCKET sock;
    > > } FTPSettings_t;
    > >
    > >
    > > When I compile I have this problem:
    > >
    > > error C2440: '=' : cannot convert from 'void *' to 'FTPSettings_t *'
    > > Conversion from 'void*' to pointer to non-'void' requires an
    > > explicit cast
    > >
    > >
    > > Please help me how to solve this problem. Thanks

    >
    > One purely technical answer is:
    >
    > void FTPServer_ThreadLoop_C( void* pSettings )
    > {
    > FTPServer_ThreadLoop( *static_cast<FTPSettings*>( pSettings ) );
    > }
    >
    > which calls the following more type safe function:
    >
    > void FTPServer_ThreadLoop( FTPSettings& settings )
    > {
    > ...
    > }
    >
    > Given that, for maintainability it can be a good idea to do something like
    >
    > struct FTPState
    > {
    > FTPState_t State;
    > SOCKADDR_STORAGE sa;
    > SOCKADDRLEN salen;
    > SOCKET sock;
    >
    > void ThreadLoop()
    > {
    > ...
    > }
    > };
    >
    > namespace g
    > {
    > FTPState* pFtpState; // Managed by code not shown here.
    > }
    >
    > void FTPServer_ThreadLoop( FTPSettings& settings )
    > {
    > g::pFtpState->ThreadLoop();
    > }
    >
    > and then notice that there's probably initialization (which should be a
    > constructor), destruction (which should be a destructor), and matters of
    > access to be granted or restricted, and so on.
    >
    > Actually I wouldn't use the namespace as shown above, and in fact the
    > code wouldn't resemble this at all. <g> I'm trying to write something
    > you'd find at least slightly familiar. Hope this helps.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
    Hakirato, Oct 5, 2006
    #4
  5. Re: Conversion from 'void*' to pointer to non-'void' requires anexplicit cast

    * Hakirato:
    > Hi
    >
    > Thanks for helping me this out..


    Well, one shouldn't post Usenet articles in the middle of the night. At
    least, I shouldn't.

    The last function should be

    void FTPServer_ThreadLoop( FTPSettings& settings )
    {
    settings.ThreadLoop();
    }

    rather than what I wrote.

    Btw., when reading this posting, do you notice any problem with the
    order the text appears in?


    >
    > //Hakirato
    >
    > Alf P. Steinbach skrev:
    >
    >> * Hakirato:
    >>> Hi
    >>>
    >>> I have this compilation problem when trying to convert C code to C++.
    >>>
    >>> My function looks like this:
    >>>
    >>> void FTPServer_ThreadLoop( void *arg_p )
    >>> {
    >>>
    >>> FTPSettings_t* FTPSettings_p;
    >>>
    >>> FTPSettings_p = arg_p;
    >>> ..
    >>> ..
    >>> ..
    >>>
    >>> }
    >>>
    >>> typedef struct
    >>> {
    >>> FTPState_t State;
    >>> SOCKADDR_STORAGE sa;
    >>> SOCKADDRLEN salen;
    >>> SOCKET sock;
    >>> } FTPSettings_t;
    >>>
    >>>
    >>> When I compile I have this problem:
    >>>
    >>> error C2440: '=' : cannot convert from 'void *' to 'FTPSettings_t *'
    >>> Conversion from 'void*' to pointer to non-'void' requires an
    >>> explicit cast
    >>>
    >>>
    >>> Please help me how to solve this problem. Thanks

    >> One purely technical answer is:
    >>
    >> void FTPServer_ThreadLoop_C( void* pSettings )
    >> {
    >> FTPServer_ThreadLoop( *static_cast<FTPSettings*>( pSettings ) );
    >> }
    >>
    >> which calls the following more type safe function:
    >>
    >> void FTPServer_ThreadLoop( FTPSettings& settings )
    >> {
    >> ...
    >> }
    >>
    >> Given that, for maintainability it can be a good idea to do something like
    >>
    >> struct FTPState
    >> {
    >> FTPState_t State;
    >> SOCKADDR_STORAGE sa;
    >> SOCKADDRLEN salen;
    >> SOCKET sock;
    >>
    >> void ThreadLoop()
    >> {
    >> ...
    >> }
    >> };
    >>
    >> namespace g
    >> {
    >> FTPState* pFtpState; // Managed by code not shown here.
    >> }
    >>
    >> void FTPServer_ThreadLoop( FTPSettings& settings )
    >> {
    >> g::pFtpState->ThreadLoop();
    >> }
    >>
    >> and then notice that there's probably initialization (which should be a
    >> constructor), destruction (which should be a destructor), and matters of
    >> access to be granted or restricted, and so on.
    >>
    >> Actually I wouldn't use the namespace as shown above, and in fact the
    >> code wouldn't resemble this at all. <g> I'm trying to write something
    >> you'd find at least slightly familiar. Hope this helps.
    >>
    >> --
    >> A: Because it messes up the order in which people normally read text.
    >> Q: Why is it such a bad thing?
    >> A: Top-posting.
    >> Q: What is the most annoying thing on usenet and in e-mail?

    >



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 5, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xxx
    Replies:
    0
    Views:
    827
  2. Dan Oprisan
    Replies:
    2
    Views:
    404
    Dan Oprisan
    Nov 20, 2003
  3. Abhishek
    Replies:
    12
    Views:
    803
    Eric Sosman
    Jan 30, 2006
  4. regis

    void * and explicit cast

    regis, Dec 1, 2008, in forum: C Programming
    Replies:
    22
    Views:
    1,023
    Tim Rentsch
    Jan 7, 2009
  5. Alf P. Steinbach /Usenet
    Replies:
    1
    Views:
    578
    James Kanze
    Jul 27, 2010
Loading...

Share This Page