THE WORLDS MOST COMPLICATED C++ ISSUE

I have created a class which uses threads and sockets but I am having trouble as the bind function works fine if I do not include the <threads> package however when I do it wrongly references bind in the "functional" package.

ERROR: Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::_Bind<false,void,int &,sockaddr *,unsigned int>' (or there is no acceptable conversion)


How do I force it to use the winsock2 bind method?

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <threads>
#include <winsock2.h>

#pragma comment(lib, "ws2_32")

class myclass {

 void connect;
 void x;
}

void myclass::connect()
{

   int host_port = 1101;

   unsigned short wVersionRequested;
   WSADATA wsaData;
   int err;
   wVersionRequested = MAKEWORD(2, 2);
   err = WSAStartup(wVersionRequested, &wsaData);
   
if (err != 0 || (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)) {
			fprintf(stderr, "Could not find useable sock dll %d\n", WSAGetLastError());
			goto FINISH;
		}

		int hsock;
		int * p_int;
		hsock = (int)socket(AF_INET, SOCK_STREAM, 0);
    if (hsock == -1)
    { 

    }

   p_int = (int*)malloc(sizeof(int));
   *p_int = 1;

   if ((setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1) || (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1))
   {   

   }

   free(p_int);

   struct sockaddr_in my_addr;

   my_addr.sin_family = AF_INET;
   my_addr.sin_port = htons((u_short)host_port);

   memset(&(my_addr.sin_zero), 0, 8);
   my_addr.sin_addr.s_addr = INADDR_ANY;


/////////// ERROR -------
   if (bind(hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR)
   {

   }


    if (listen(hsock, 10) == -1)
    {  
    
    }
}

void myclass::x() {
    std::thread first(&myclass::x, this);
}

Not sure why it picks std::bind but I think you can solve it by putting :: in front of the function name.
 
if (::bind(hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR)
Last edited on
Here are a few ides to try:

1) Add using ::bind;
2) Change line 56 to if ( ::bind(hsock, ...
3) have void myclass:x() in a different source file. Don't include <thread> in the current file.
4) Put a namespace around #include <winsock>.
1
2
3
4
5
namespace ws
{ // Though this may cause linker errors
  #include <winsock2.h>
}
using ws::bind;
Thanks so much guys, adding the :: before the bind sorted it!!
Topic archived. No new replies allowed.