Compiling with gcc fails if using lambda function for QObject::connect()

This Code compiles without any problems Qt5.2.0 MSVC2012:

#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();
}

But if I try to compile it with Qt5.2.0 with gcc I get this errors:

../_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 argumen ts, 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 typena me 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/QtCo re/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 fai led: ../_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.

Where is the problem?


如注释中所回答的那样,使用-std = c ++ 11让它使用gcc进行编译

链接地址: http://www.djcxy.com/p/29756.html

上一篇: std ::绑定一个绑定函数

下一篇: 如果使用QObject :: connect()的lambda函数编译gcc失败