2
votes

I'm using Qt 5.3 with MinGW 4.8.2 (due to client restrictions the Qt version cannot be upgraded). Trying to connect a QTcpSocket with a lambda expression ends up in a compiler error.

I have imported <QTcpSocket> and my class inherits QObject publicly. The Q_OBJECT macro has also been added to the header file.

This is how I am trying to connect the socket to a lambda function:

void TCPRequests::handleClient() {
    QTcpSocket* sock = serv -> nextPendingConnection();
    connect(sock, &QTcpSocket::readyRead, [sock]()
    {
        // Do nothing
    });
}

This compiles and works correctly on Qt 5.8 but not on 5.3 with MinGW 4.8.2.

I have also tried to do connect(sock, &QTcpSocket::readyRead, this, [sock]() {...}); (note I am also passing this as the third argument), but it makes no difference.

The resulting errors are:

First:

D:\Documents\Development\X\TCPRequests.cpp:43: error: no matching function for call to 'TCPRequests::connect(QTcpSocket*&, void (QIODevice::*)(), TCPRequests::handleClient()::__lambda0)' });

Second:

D:\Documents\Development\X\TCPRequests.cpp:43: error: template argument for 'template static typename QtPrivate::QEnableIf<((int)(QtPrivate::FunctionPointer::ArgumentCount) >= 0), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, Func2)' uses local type 'TCPRequests::handleClient()::__lambda0' });

Third:

D:\Documents\Development\X\TCPRequests.cpp:43: error: template argument for 'template static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, Func2)' uses local type 'TCPRequests::handleClient()::__lambda0' });

Any help would be very appreciated!

1

1 Answers

2
votes

You probably want to add CONFIG += c++11 to your project file (.pro file) to enable c++11 features in your project.

The reason why not giving a error in Qt 5.8 is because starting in Qt 5.7.0 the c++11 is enabled by default and old compilers that doesn't supports c++11 are not supported. See the note:

C++11 Support Required from the compiler

Qt has enabled usage of C++11 in Qt applications for a long time, but with Qt 5.7 we are also enabling use of C++11 in the Qt modules. Therefore Qt 5.7 requires C++11 support from the compiler, and has removed support from older compilers not providing adequate C++11 support.