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

此代码编译没有任何问题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();
}

但是,如果我尝试用gcc编译Qt5.2.0,我得到这个错误:

../_test_gccLambda/main.cpp:在函数'int main(int,char **)'中:../_test_gccLambda/main.cpp:14:5:warning:lambda表达式仅适用于-std = c ++ 11或-std = gnu ++ 11 [默认启用]}); (QTimer ::)(QTimer :: QPrivateSignal),main(int,char *)无效。 ):: __ lambda0)'}); ^ ../_test_gccLambda/main.cpp:14:6:note:candidates are:包含在../../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 expect 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 expected 5个理由 ts,3提供../../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:注意:没有将参数2从'void(QTimer ::)(QTimer :: QPrivateSignal)'转换为'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 我QtPrivate :: FunctionPointer :: Object *'和'main(int,char **):: __ lambda0'}); ^从../../Qt5/5.2.0/gcc_64/include/QtCore/qcoreapplication.h:48:0包含的文件中,从../../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 /替换失败:../_test_gccLambda/main.cpp:替换模板静态类型名称QtPrivate :: QEnableIf <((int)(QtPrivate :: FunctionPointer :: ArgumentCount)> = 0),QMetaObject :: Connection> ::类型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:从这里需要../_test_gccLambda/main.cpp:14:6:error:template argument for'模板静态类型名称QtPrivate :: QEnableIf <((int)(QtPrivate :: FunctionPointer :: ArgumentCount)> = 0),QMetaObject :: Connection> :: Type QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *, Func1,Func2)'使用本地类型'main(int,char **):: __ lambda0'}); ^ _/_test_gccLambda/main.cpp:14:6:错误:试图实例化模板静态类型名QtPrivate :: QEnableIf <((int)(QtPrivate :: FunctionPointer :: ArgumentCount)> = 0),QMetaObject :: Connection > :: Type QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1,Func2)'从../../Qt5/5.2.0/gcc_64/include/QtCore/qcoreapplication.h包含的文件中:48:0,来自../../Qt5/5.2.0/gcc_64/include/QtCore/QCoreApplication:1,来自../_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 :: (const typename QtPrivate :: FunctionPointer :: Object *,Func1,const QObject *,Func2,Qt :: ConnectionType)connect(const typename QtPrivate :: QPlayterToMemberFunction)),QMetaObject :: Connection> :: Type QObject :: connect :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:can not convert'main(int,char **):: __ lambda0 {} '(type'main(int,char **):: __ lambda0')来键入'const QObject *'}); ^从../../Qt5/5.2.0/gcc_64/include/QtCore/qcoreapplication.h:48:0包含的文件中,从../../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失败:../_test_gccLambda/main.cpp:替换'模板静态类型名称QtPrivate :: QEnableIf <(QtPrivate :: FunctionPointer :: ArgumentCount ==(-1)),QMetaObject :: Connection> :: Type QObject ::连接(const typename QtPrivate :: FunctionPointer :: Object *,Func1,Func2)[with Func1 = void(QTimer ::)(QTimer :: QPrivateSignal); Func2 = main(int,char *):: __ lambda0]':../_test_gccLambda/main.cpp:14:6:从这里需要../_test_gccLambda/main.cpp:14:6:error:template argument for' (QtPrivate :: FunctionPointer :: ArgumentCount ==(-1)),QMetaObject :: Connection> :: Type QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1, Func2)'使用本地类型'main(int,char **):: __ lambda0'}); ^ ../_test_gccLambda/main.cpp:14:6:错误:试图实例化'模板静态类型名QtPrivate :: QEnableIf <(QtPrivate :: FunctionPointer :: ArgumentCount ==(-1)),QMetaObject :: Connection>: :类型QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1,Func2)'从../../Qt5/5.2.0/gcc_64/include/QtCore/qcoreapplication.h:48包含的文件中:0,来自../../Qt5/5.2.0/gcc_64/include/QtCore/QCoreApplication:1,来自../_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: :const(const typename QtPrivate :: FunctionPointer :: Object *,Func1,const QObject *,Func2,Qt :: ConnectionType)connect(const typename QtPrivate :: FunctionPointer :: Object * sender,Func1 signal,const QObject * context,Func2插槽,^ ../../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:无法转换'main(int,char **):: __ lambda0 {}'(type'main(int,char **):: __ lambda0')键入'const QObject *'}); ^ make:* [main.o]错误1 16:53:24:使用代码2退出进程“/ usr / bin / make”。构建/部署项目时出错_test_gccLambda(套件:Desktop Qt 5.2.0 GCC 64bit)执行步骤'Make'16:53:24时:已用时间:00:02。

哪里有问题?


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

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

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

下一篇: Compiling boost::move in gcc 4.6.4