所有

我有一些代码:

if (bind(this->socketListen, (SOCKADDR*)& this->addr, sizeof(this->addr)) == SOCKET_ERROR)
{
   cerr << "Failed to bind the address to our listening socket. Winsock
            Error:" << to_string(WSAGetLastError()) << endl;
   exit(1);
}


此代码的第一行生成错误:

E0349 no operator "==" matches these oparands



C2678 binary '==': no operator found which takes a left-hand operand of type 'std::_Binder<std::_Unforces,SOCKET &,SOCKADDR *,unsigned int>'(or there is no acceptable conversion)

我该如何解决?

最佳答案

看起来编译器很容易将bind()调用与std::bind混淆。最有可能的原因是您之前写过using namespace std;,并且包含了<functional>标头。

最简单的解决方法是,您可以明确地告诉您要从全局名称空间获取bind()

if (::bind(this->socketListen, (SOCKADDR*)& this->addr, sizeof(this->addr)) == SOCKET_ERROR)
 // ^^


最好是摆脱using namespace std;并使用特定的using语句来满足namespace std的需要,例如

 using std::cin;


或在需要的地方简单地添加std::前缀。

10-06 13:38
查看更多