I was writing a class to simplify networking stuff, so i defined a connect() function like so:
int connect(string ip, unsignedint 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?
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 usingnamespace type statements? They are usually the culprits for this sort of problem.
#include <string>
#include <iostream>
#include <winsock2.h>
int connect(std::string ip, unsignedint port);
/*****************************************************************************/
int main()
{
int temp = connect("127.0.0.1", 8080);
}
/*****************************************************************************/
int connect(std::string ip, unsignedint 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;
}
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.
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.