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

H

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
 
H

Howard

Hakirato said:
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
 
A

Alf P. Steinbach

* 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

Alf P. Steinbach

* 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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top