2017-02-12 3 views
2

私はMinGW 4.8.2でQt 5.3を使用しています(クライアントの制限により、Qtバージョンはアップグレードできません)。 connectにラムダ式を持つQTcpSocketがコンパイルエラーで終了します。Qt 5.3ラムダと接続

私は<QTcpSocket>をインポートし、私のクラスはQObjectを一般に継承しています。 Q_OBJECTマクロもヘッダーファイルに追加されています。

これは、私はラムダ関数にソケットを接続しようとしています方法です:

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

これは、コンパイルし、MinGWの4.8.2でのQt 5.8でなく5.3で正常に動作します。

私はまた、 connect(sock, &QTcpSocket::readyRead, this, [sock]() {...});(第3引数として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

プロジェクトでC++ 11の機能を有効にするには、CONFIG += c++11をプロジェクトファイル(.proファイル)に追加することをお勧めします。

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

関連する問題