2017-02-12 99 views
2

我在MinGW 4.8.2中使用Qt 5.3(由於客戶端限制Qt版本無法升級)。 嘗試connect帶有lambda表達式的QTcpSocket最終出現編譯器錯誤。Qt 5.3與lambda連接

我導入了<QTcpSocket>,我的班級公開繼承QObjectQ_OBJECT宏也被添加到頭文件中。

這是我正在試圖將套接字連接到lambda函數:

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

這編譯和正確適用於Qt的5.8,但不是在5.3 MinGW的4.8.2。

我也試過做 connect(sock, &QTcpSocket::readyRead, this, [sock]() {...});(注意我也是通過this作爲第三個參數),但沒有區別。

產生的錯誤是:

第一:

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

二:

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' });

三:

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' });

任何幫助將是非常讚賞!

回答

2

您可能需要將CONFIG += c++11添加到您的項目文件(.pro文件)以啓用項目中的C++ 11功能。

Qt 5.8中不給出錯誤的原因是因爲從Qt 5.7.0開始,C++ 11默認啓用,不支持不支持C++ 11的舊編譯器。查看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.

+0

非常感謝!在這種情況下,Qt Creator會提醒你很高興 – vagaerg