2016-01-25 97 views
-1

我不明白爲什麼我得到這個錯誤,但同樣的類在VS15下工作完美現在我使用VS12,這是一個簡單的Winsock2實現,錯誤C2678:二進制'==':找不到操作符需要左手操作數

int Net::createServer(int port, int protocol) 
{ 
    int status; 

    // ----- Initialize network stuff ----- 
    status = initialize(port, protocol); 
    if (status != NET_OK) 
     return status; 

    localAddr.sin_addr.s_addr = htonl(INADDR_ANY); // listen on all addresses 

    // bind socket 
    if (bind(sock, (SOCKADDR *)&localAddr, sizeof(localAddr)) == SOCKET_ERROR) 
    { 
     status = WSAGetLastError();   // get detailed error 
     return ((status << 16) + NET_BIND_FAILED); 
    } 
    bound = true; 
    mode = SERVER; 

    return NET_OK; 
} 

問題來源於這裏

if (bind(sock, (SOCKADDR *)&localAddr, sizeof(localAddr)) == SOCKET_ERROR) 

控制檯日誌:

error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>' (or there is no acceptable conversion) 
1>   with 
1>   [ 
1>    _Forced=false, 
1>    _Ret=void, 
1>    _Fun=SOCKET &, 
1>    _V0_t=SOCKADDR *, 
1>    _V1_t=size_t, 
1>    _V2_t=std::_Nil, 
1>    _V3_t=std::_Nil, 
1>    _V4_t=std::_Nil, 
1>    _V5_t=std::_Nil, 
1>    <unnamed-symbol>=std::_Nil 
1>   ] 
+2

使用_Winsock_你的代碼的Ubuntu下工作?令人印象深刻... –

+0

是的,這是一個錯誤 – Eddy

+1

請發表[mcve],而不是這麼大的代碼轉儲。此外,你的包括警衛是非法的:https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier –

回答

6

(A位這裏信仰的飛躍,但我很確定我是對的)。 錯誤的原因是您的程序中某處存在using namespace std語句(這是可怕的事情!),現在BSD套接字bind()函數與STL std::bind函數衝突。

停止使用using namespace std陳述一勞永逸,問題就會消失。否則,你有資格去拉它命名bind()從,在這種情況下,全局命名空間:

if (::bind(sock, (SOCKADDR *)&localAddr, sizeof(localAddr)) == SOCKET_ERROR) 
+0

這個解決了它,乾杯:) – Eddy

相關問題