Socket bind() error: invalid operands to binary expression

本文引用自StackOverflow。

原始代码Code:

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

报错Error:

fs_server.cpp:264:56: error: invalid operands to binary expression ('__bind<int &, sockaddr *,
      unsigned long>' 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?

我用C++11\17在Linux上跑的好好的,但是放在Mac上用C++17就不行了。为啥呢?

解决方案:

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) {
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值