[英]Error 3 error C2440: 'initializing' : cannot convert from 'void *' to 'Socket *'
I have problem when I implamante default constructor but I have error我在implamante默认构造函数时遇到问题但我有错误
Error 3 error C2440: 'initializing' : cannot convert from 'void *' to 'Socket *' Webserver.h 164 1 Project2_SocketLib
错误 3 错误 C2440:“正在初始化”:无法从“void *”转换为“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 *
为什么你明确地将类型转换为
void *
Socket* ptr_s =(void*) in.Accept();
Should be,应该,
Socket sock = in.Accept();
Accept
returns SOCKET. Accept
返回 SOCKET。 No need to convert it to void *
or Socket *
无需将其转换为
void *
或Socket *
我希望这会奏效
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.在 C++ 中,与 C 不同,
void*
不能隐式转换为指向对象的指针。 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.注意:比 C 风格的强制转换更喜欢 C++ 风格的强制转换。 They are more explicit in their intent and they are easier to search for.
它们的意图更明确,更容易搜索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.