此代碼編譯沒有任何問題Qt5.2.0 MSVC2012:用gcc編譯,如果使用lambda功能的QObject ::連接失敗()
#include <QCoreApplication>
#include <QObject>
#include <QTimer>
#include <QDebug>
int main(int argc, char *argv[])
{
QTimer timer;
QCoreApplication a(argc, argv);
QObject::connect(&timer, &QTimer::timeout,
[=]() {
qDebug() << Q_FUNC_INFO;
});
timer.start(1000);
return a.exec();
}
但如果我嘗試用gcc與Qt5.2.0編譯它,我得到這個錯誤:
../_test_gccLambda/main.cpp: In function 'int main(int, char**)': ../_test_gccLambda/main.cpp:14:5: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default] }); ^ ../_test_gccLambda/main.cpp:14:6: error: no matching function for call to 'QObject::connect(QTimer*, void (QTimer::)(QTimer::QPrivateSignal), main(int, char*)::__lambda0)' }); ^ ../_test_gccLambda/main.cpp:14:6: note: candidates are: In file included from ../../Qt5/5.2.0/gcc_64/include/QtCore/qcoreapplication.h:48:0, from ../../Qt5/5.2.0/gcc_64/include/QtCore/QCoreApplication:1, from ../_test_gccLambda/main.cpp:1: ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:198:36: note: static QMetaObject::Connection QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType) static QMetaObject::Connection connect(const QObject *sender, const char *signal, ^ ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:198:36: note: candidate expects 5 arguments, 3 provided ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:201:36: note: static QMetaObject::Connection QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType) static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, ^ ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:201:36: note: candidate expects 5 arguments, 3 provided ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:479:32: note: QMetaObject::Connection QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal, ^ ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:479:32: note: no known conversion for argument 2 from 'void (QTimer::)(QTimer::QPrivateSignal)' to 'const char' ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:215:43: note: template static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, const typename QtPrivate::FunctionPointer::Object*, Func2, Qt::ConnectionType) static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, ^ ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:215:43: note: template argument deduction/substitution failed: ../_test_gccLambda/main.cpp:14:6: note: mismatched types 'const typename QtPrivate::FunctionPointer::Object*' and 'main(int, char**)::__lambda0' }); ^ In file included from ../../Qt5/5.2.0/gcc_64/include/QtCore/qcoreapplication.h:48:0, from ../../Qt5/5.2.0/gcc_64/include/QtCore/QCoreApplication:1, from ../_test_gccLambda/main.cpp:1: ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:247:13: note: template static typename QtPrivate::QEnableIf<((int)(QtPrivate::FunctionPointer::ArgumentCount) >= 0), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, Func2) connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, Func2 slot) ^ ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:247:13: note: template argument deduction/substitution failed: ../_test_gccLambda/main.cpp: In substitution of 'template static typename QtPrivate::QEnableIf<((int)(QtPrivate::FunctionPointer::ArgumentCount) >= 0), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, Func2) [with Func1 = void (QTimer::)(QTimer::QPrivateSignal); Func2 = main(int, char*)::__lambda0]': ../_test_gccLambda/main.cpp:14:6: required from here ../_test_gccLambda/main.cpp:14:6: 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 'main(int, char**)::__lambda0' }); ^ ../_test_gccLambda/main.cpp:14:6: error: trying to instantiate 'template static typename QtPrivate::QEnableIf<((int)(QtPrivate::FunctionPointer::ArgumentCount) >= 0), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, Func2)' In file included from ../../Qt5/5.2.0/gcc_64/include/QtCore/qcoreapplication.h:48:0, from ../../Qt5/5.2.0/gcc_64/include/QtCore/QCoreApplication:1, from ../_test_gccLambda/main.cpp:1: ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:256:13: note: template static typename QtPrivate::QEnableIf<(((int)(QtPrivate::FunctionPointer::ArgumentCount) >= 0) && (! QtPrivate::FunctionPointer::IsPointerToMemberFunction)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, const QObject*, Func2, Qt::ConnectionType) connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, const QObject *context, Func2 slot, ^ ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:256:13: note: template argument deduction/substitution failed: ../_test_gccLambda/main.cpp:14:6: note: cannot convert 'main(int, char**)::__lambda0{}' (type 'main(int, char**)::__lambda0') to type 'const QObject*' }); ^ In file included from ../../Qt5/5.2.0/gcc_64/include/QtCore/qcoreapplication.h:48:0, from ../../Qt5/5.2.0/gcc_64/include/QtCore/QCoreApplication:1, from ../_test_gccLambda/main.cpp:1: ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:287:13: note: template static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, Func2) connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, Func2 slot) ^ ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:287:13: note: template argument deduction/substitution failed: ../_test_gccLambda/main.cpp: In substitution of 'template static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, Func2) [with Func1 = void (QTimer::)(QTimer::QPrivateSignal); Func2 = main(int, char*)::__lambda0]': ../_test_gccLambda/main.cpp:14:6: required from here ../_test_gccLambda/main.cpp:14:6: 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 'main(int, char**)::__lambda0' }); ^ ../_test_gccLambda/main.cpp:14:6: error: trying to instantiate 'template static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, Func2)' In file included from ../../Qt5/5.2.0/gcc_64/include/QtCore/qcoreapplication.h:48:0, from ../../Qt5/5.2.0/gcc_64/include/QtCore/QCoreApplication:1, from ../_test_gccLambda/main.cpp:1: ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:295:13: note: template static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer::Object*, Func1, const QObject*, Func2, Qt::ConnectionType) connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, const QObject *context, Func2 slot, ^ ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:295:13: note: template argument deduction/substitution failed: ../_test_gccLambda/main.cpp:14:6: note: cannot convert 'main(int, char**)::__lambda0{}' (type 'main(int, char**)::__lambda0') to type 'const QObject*' }); ^ make: * [main.o] Error 1 16:53:24: The process "/usr/bin/make" exited with code 2. Error while building/deploying project _test_gccLambda (kit: Desktop Qt 5.2.0 GCC 64bit) When executing step 'Make' 16:53:24: Elapsed time: 00:02.
問題在哪裏?
使用-std = C++ 11模式編譯。見http://stackoverflow.com/questions/17299139/building-qt-project-for-c11-standard – Khurshid
'CONFIG + = C++ 11',如果你使用QMake –
它的工作原理。謝謝! – avb