Reputation: 41
A snippet of my code looks as follows:
int descriptor = socket(AF_INET, SOCK_STREAM, 0);
if(descriptor < 0){
cerr << "Error establishing socket connection." << endl;
return -1;
}
int port = 3400;
struct sockaddr_in address;
char buffer[140];
address.sin_family = AF_INET;
address.sin_addr.s_addr = htons(INADDR_ANY);
address.sin_port = htons(port);
int size = sizeof(address);
if(bind(descriptor,(struct sockaddr*)&address,size) < 0){
cerr << "Error binding socket." << endl;
}
cout << "Waiting for connection on " << INADDR_ANY << " on port " << port << ends;
whenever I try compiling this, I get the following error:
error: invalid operands to binary expression
('__bind<int &, sockaddr *, int &>' and 'int')
if(bind(descriptor,(struct sockaddr*)&address,size) < 0){
Does anybody know what this could mean? bind()
is supposed to return an integer or so I thought. My imports look like this:
#include <iostream>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
using namespace std;
Thanks!
Upvotes: 3
Views: 1202
Reputation: 35901
using namespace std;
combined with too much header inclusions is likely the culprit here - there's a reason why it has been repeated over and over again here on SO not to use it. By doing that, the compiler sees bind
and thinks you mean std::bind
from <functional>
, not ::bind
for sockets. So either do the right thing and review if you really need to include all those headers and get rid of that using declaration, or use ::bind
(edit: or both - it's not bad to use ::
to indicate you want to use some standard API function from the global namespace)
Upvotes: 15