Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket bind() error: invalid operands to binary expression

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?

like image 382
Morgan Avatar asked Jul 17 '26 08:07

Morgan


1 Answers

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.

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:

if (::bind(sockfd, (sockaddr *) &addr, sizeof(addr)) == -1) {
like image 135
Remy Lebeau Avatar answered Jul 19 '26 22:07

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!