Why you are explicitly type casting to void *
Socket* ptr_s =(void*) in.Accept();
Should be,
Socket sock = in.Accept();
Accept
returns SOCKET. No need to convert it to void *
or Socket *
سؤال
I have problem when I implamante default constructor but I have error
Error 3 error C2440: 'initializing' : cannot convert from 'void *' to 'Socket *' Webserver.h 164 1 Project2_SocketLib
Socket.h
//SOCKET Accept(sockaddr* clientInfo,int* clientInfoSize)
SOCKET Accept()
{
static int size = sizeof(sockaddr);
return accept(this->hSocket, 0,0);
}
Webserver.h
Webserver(short port_to_listen,request_func rf,HWND Hwnd, WPARAM wParam, LPARAM lParam)
{
Socket in(port_to_listen,"INADDR_ANY", true, Hwnd, true);
//request_func = rf;
while (1) {
Socket* ptr_s =(void*) in.Accept();
unsigned ret;
_beginthreadex(0,0,Request, ptr_s,0,&ret);
}
}
المحلول
Why you are explicitly type casting to void *
Socket* ptr_s =(void*) in.Accept();
Should be,
Socket sock = in.Accept();
Accept
returns SOCKET. No need to convert it to void *
or Socket *
نصائح أخرى
I hope this will work
Socket* ptr_s =static_cast<Socket*>( in.Accept());
In C++, unlike in C, a void*
can't be implicitly converted to a pointer to object. Instead, you should cast directly to the target pointer type:
Socket* ptr_s = reinterpret_cast<Socket*>(in.Accept());
Note: Prefer C++-style casts over C-style casts. They are more explicit in their intent and they are easier to search for.