【Socket bind() error: invalid operands to binary expression】【转载】

原始代码Code:

if (bind(sockfd, (sockaddr *) &addr, sizeof(addr)) == -1) {

报错Error:

error: invalid operands to binary expression ('__bind' and 'int')     
if (bind(sockfd, (sockaddr *) &addr, sizeof(addr)) == -1) {

问题描述:

This was working when I run this code using C++11, and also works when I run this code using C++ 17 on Linux. It does not work when I run it on Mac OS using C++17. To me, it looks like the error indicates that the comparison is between the function itself and -1. I’m not sure why this would happen. Am I interpreting something incorrectly?

解决方案: 

Your code is trying to call the C++ ​​std::bind()​​​ function, not the socket ​​bind()​​​ function. This is likely due to a ​​using namespace std;​​ statement in your code.

你的代码里应该用了​​using namespace std;​​​的声明,导致编译器认为你调用的是​​std::bind()​​​,而不是socket包下面的​​bind()​​。

To ensure the correct function is being called, you can either get rid of the using statement, or else qualify the call as using the function from the global namespace:

解决办法也很简单,要不把​​using namespace std;​​​全清理掉,要不就用全局的namespace调用socket的​​bind()​​,代码改成这样:

if (::bind(sockfd, (sockaddr *) &addr, sizeof(addr)) == -1) {

原文链接:
Socket bind() error: invalid operands to binary expression_11874324的技术博客_51CTO博客

你可能感兴趣的:(编译问题,服务器,linux,网络)