PDA

View Full Version : VS2008 build error for connect function



yektaayduk
11th February 2011, 09:25
I'm developing a client server application using winsock2.h functions of windows.
But I have also some Gui's which are coded in QT 4.7 in the project.

This part of the code is casue a build error:

code :


// Connect to server
iResult= connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if (iResult == SOCKET_ERROR)
{
printf( "Failed to connect.\n" );
WSACleanup();
}

build error:

1>.\client_thread.cpp(80) : error C2664: 'bool QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const' : cannot convert parameter 1 from 'SOCKET' to 'const QObject *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

Compiler is assuming that the connect function is the function of QT (connect signal slot).

How can I solve this ?

high_flyer
11th February 2011, 09:29
How can I solve this ?
By explicitly using the correct scope with '::'.

yektaayduk
11th February 2011, 09:52
I changed the code as below:

iResult= ::connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );

"You can tell the compiler to use the global identifier rather than the local identifier by prefixing the identifier with ::, the scope resolution operator."

Ä°t's working now .But Im not sure that winsock connect function is global identifier and Qt connect function is not.

high_flyer
11th February 2011, 09:57
But Im not sure that winsock connect function is global identifier and Qt connect function is not.
Check the docs, and you will be sure.
Or go to the definition, and you will see which function is being called.
Another way is to use defines (in such cases of conflicting names) like:
#define qconnect ::connect
Or just explicit scopes everywhere where such conflicts are probable, so the QObject connect is then QObejct::connect your socket connect is ::connect and all is well.