Function overloading error

I was writing a class to simplify networking stuff, so i defined a connect() function like so:

int connect(string ip, unsigned int port);

now in the code for it, there is a line where i make a call back to the connect() function defined in winsock.h, which uses a different list of arguments so i should be able to "overload" the function.

the line in question:
if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0)

But the Microsoft Visual Studio compiler complains with the following error:

error C2660: 'GameSocket::connect' : function does not take 3 arguments

but in winsock.h, connect does most definitely take 3 arguments... so what am i doing wrong?
Last edited on
It sounds like your compiler has picked up the connect method(s) in the GameSocket class rather than the one defined in winsock. Somewhere it is being told to this. Have you got any using namespace type statements? They are usually the culprits for this sort of problem.
I did have using namespace std; in both the header and source files, so I tried removing them, and the compiler still gave the same error.
closed account (z05DSL3A)
Probably need to see more code. If there is a lot of code, try to produce something small that still shows the error your having, along the lines of:

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
#include <string>
#include <iostream>
#include <winsock2.h>

int connect(std::string ip, unsigned int port);

/*****************************************************************************/
int main() 
{
	int temp = connect("127.0.0.1", 8080);

}
/*****************************************************************************/

int connect(std::string ip, unsigned int port)
{
	WSADATA wsaData;
	  int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
	  if (iResult != NO_ERROR)
		printf("Error at WSAStartup()\n");

	  //----------------------
	  // Create a SOCKET for connecting to server
	  SOCKET ConnectSocket;
	  ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	  if (ConnectSocket == INVALID_SOCKET) {
		printf("Error at socket(): %ld\n", WSAGetLastError());
		WSACleanup();
		return -1;
	  }

	  //----------------------
	  // The sockaddr_in structure specifies the address family,
	  // IP address, and port of the server to be connected to.
	  sockaddr_in clientService; 
	  clientService.sin_family = AF_INET;
	  clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
	  clientService.sin_port = htons( 27015 );


	if ( connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) 
	{
		printf( "Failed to connect.\n" );
		WSACleanup();
		return-1;
	}
	return 0;
}
in the header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _GAMESOCKET_H_
#define _GAMESOCKET_H_

#include <winsock2.h>
#include <string>
class GameSocket
{
	private:
		SOCKET sock;
		struct sockaddr_in addr;
		struct hostent *hostInfo;

	public:
		bool connect(std::string ip, unsigned short port);
};
#endif 


in the .cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <winsock2.h>
#include "GameSocket.h"

bool GameSocket::connect(std::string ip, unsigned short port)
{
	hostInfo = gethostbyname(ip.c_str());
	if (hostInfo == NULL) return FALSE;
	
	addr.sin_family = hostInfo->h_addrtype;
	addr.sin_port = htons(port);
	memcpy((char *) &addr.sin_addr.s_addr, hostInfo->h_addr_list[0], hostInfo->h_length);

	if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0)
	{
		closesocket(sock);
		return FALSE;
	}

	return TRUE;
}


I do have the wsaData initialization (its just in a seperate function) and i am also linking in the library correctly.

Also, if i take the connect() function that you posted, and put it inside of the class, the same error occurs.
Last edited on
Thats your problem

It will always default to the class you're in. Probably changing the name of GameSocket::connect() to something else e.g. connectSocket() or specify the winsock connect call to its class, I presume winsock::connect either should fix it.
Last edited on
winsock is in C, so it has no class
so.. there is no way around changing the name of the function?
Then change the name of your class method from connect to something else

EDIT: C code isn't smart enough to handle overloading/conflict type issues so avoid them where you can.
Last edited on
So by accident, i discovered that if you put :: in front of a variable/function, it apparently causes the compiler to use the global version of the variable/function... so i just put a :: in front of the connet(sock, ...) and it now compiles fine.
Last edited on
closed account (z05DSL3A)
I was just about to suggest using gobal scope resolution. Glad you got it sorted.
I should have spotted that one.

Glad you got it sorted out.
Topic archived. No new replies allowed.