简体   繁体   English

错误 3 错误 C2440:“正在初始化”:无法从“void *”转换为“Socket *”

[英]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.

相关问题 错误C2440:“正在初始化”:无法从“ LPVOID”转换为“ UINT” - error C2440: 'initializing' : cannot convert from 'LPVOID' to 'UINT 错误 C2440:“正在初始化”:无法从“CTable”转换为“CTable” - Error C2440: 'initializing': cannot convert from 'CTable' to 'CTable' 错误 C2440:“正在初始化”:无法从 - error C2440: 'initializing' : cannot convert from 错误C2440:“正在初始化”:无法从“类名*”转换为“类名” - error C2440: 'initializing' : cannot convert from 'classname *' to 'classname' C ++错误C2440:“正在初始化”:无法从“类名”转换为“相同的类名” - C++ error C2440: 'initializing': cannot convert from 'class name' to 'same class name' 错误C2440:“正在初始化”:无法从“ std :: _ A_iterator &lt;_B&gt;”转换为“ std :: _ A_iterator &lt;_B&gt;” - error C2440: 'initializing' : cannot convert from 'std::_A_iterator<_B>' to 'std::_A_iterator<_B>' 错误C2440:“正在初始化”:无法从“ std :: _ Vector_iterator &lt;_Ty,_Alloc&gt;”转换为“类型*” - error C2440: 'initializing' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'type *' 错误C2440:“正在初始化”:在Visual Studio 2008中无法从“ const jchar *”转换为“ LPCWSTR” - error C2440: 'initializing' : cannot convert from 'const jchar *' to 'LPCWSTR' in Visual Studio 2008 错误C2440:&#39;初始化&#39;:无法从&#39;const int&#39;转换为&#39;int *&#39; - error C2440: 'initializing' : cannot convert from 'const int' to 'int *' 错误C2440:“正在初始化”:无法从“ const wchar_t [9]”转换为“ LPCSTR” - error C2440: 'initializing' : cannot convert from 'const wchar_t [9]' to 'LPCSTR'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM