Hey,

I have a bit of a problem. I have a class that uses C libraries to make a TCP connection, which is accomplished with the connect() function. Now, I'd like to use this class in a Qt program and make it inherit QObject. The problem is, since I need signals and slots, I inherit the connect() function from QObject, which overrides the C function, therefore causing problems while compiling.

So, is there a way to make an explicit call to the "TCP" connect()?

Dani AI

Generated

Short answer: call the C/POSIX connect from the global namespace (or move the socket code into a separate wrapper) so it does not get hidden by QObject's signal/slot overloads. pointed out qualification for class methods; for plain C functions the standard solution is the global-namespace qualifier.

A minimal example that shows the idea (POSIX/Linux style):

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int fd = ::socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sa = {};
sa.sin_family = AF_INET;
sa.sin_port = htons(12345);
inet_pton(AF_INET, "1.2.3.4", &sa.sin_addr);

if (::connect(fd, reinterpret_cast<struct sockaddr*>(&sa), sizeof(sa)) < 0) {
    // handle errno
}

Practical notes and safer alternatives: avoid doing a blocking connect inside a QObject constructor (constructor blocking freezes the event loop). Prefer one of: use QTcpSocket (integrates with Qt signals/slots), perform the POSIX connect from a helper/wrapper class so the public API uses distinct names, or run socket operations in a worker thread / use non-blocking connect + select/poll or QSocketNotifier. On Windows the same name conflict exists with winsock's connect; include the proper headers and call WSAStartup before socket calls.

For 's situation the quickest fix is the global qualifier (::connect). For a cleaner, Qt-friendly design, wrap raw sockets or switch to QTcpSocket and move long-running I/O out of constructors to avoid subtle runtime problems.

Recommended Answers

All 7 Replies

Qualify it with the name of the TCP base class: MyTCPBaseClassName::connect()

Qualify it with the name of the TCP base class: MyTCPBaseClassName::connect()

Ehm, how do you do that, exactly, with C libraries?

I have a class that uses C libraries to make a TCP connection

I assumed that this meant that you had a wrapper for your C functions. If not, can you modify the TCP class?

I assumed that this meant that you had a wrapper for your C functions. If not, can you modify the TCP class?

No, I don't have a wrapper class for the C functions. I copied some code I've made some time ago to a C++ class "StatusConnection" which inherits QObject. The "StatusConnection" tries to establish the TCP connection in the constructor part.

Sorry, I had misread your initial post. Hmm. Well, since the C code belongs to you, is there anything stopping you from doing a search and replace on it. I imagine there is a way to do what you want but I'm not sure what it is.

Sorry, I had misread your initial post. Hmm. Well, since the C code belongs to you, is there anything stopping you from doing a search and replace on it. I imagine there is a way to do what you want but I'm not sure what it is.

Ehm, I don't have the original C code of the function, it's in the sys/types.h and sys/socket.h libraries. I only call the function, but it happens to be of the same name as the QObject's connect(). What I need is a workaround to be able to distinguish these two functions so I can call them both from the same class.

Let's see if anyone else has some insight as to whether this is possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.