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

时间:2014-02-10 15:56:38

标签: c++ qt gcc c++11 lambda

本规范编译没有任何问题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();
}

但是如果我尝试使用gt用Qt5.2.0编译它我会得到这个错误:

  

../_ test_gccLambda / main.cpp:在函数&#39; int main(int,char **)&#39;:   ../_test_gccLambda/main.cpp:14:5:警告:lambda表达式仅适用于-std = c ++ 11或-std = gnu ++ 11 [默认启用]        });        ^   ../_test_gccLambda/main.cpp:14:6:错误:没有匹配函数来调用&#39; QObject :: connect(QTimer *,void(QTimer :: )(QTimer :: QPrivateSignal),main (int,char *):: __ lambda0)&#39;        });         ^   ../_test_gccLambda/main.cpp:14:6:注意:候选人是:   在../../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:198:36:注意:静态QMetaObject ::连接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:注意:候选人需要5个参数,3个提供   ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:201:36:注意:静态QMetaObject ::连接QObject :: connect(const QObject *,const QMetaMethod&amp;,const QObject *, const QMetaMethod&amp;,Qt :: ConnectionType)        static QMetaObject :: Connection connect(const QObject * sender,const QMetaMethod&amp; signal,                                       ^   ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:201:36:注意:候选人需要5个参数,3个提供   ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:479:32:注意:QMetaObject :: Connection QObject :: connect(const QObject *,const char *,const char *,Qt :: ConnectionType)const    内联QMetaObject ::连接QObject :: connect(const QObject * asender,const char * asignal,                                   ^   ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:479:32:注意:来自&#39; void(QTimer :: )的参数2没有已知的转换(QTimer) :: QPrivateSignal)&#39; to&#39; const char &#39;   ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:215:43:注意:模板静态QMetaObject ::连接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:注意:模板参数扣除/替换失败:   ../_test_gccLambda/main.cpp:14:6:注意:不匹配的类型&#const; const typename QtPrivate :: FunctionPointer :: Object *&#39;和&#39; main(int,char **):: __ lambda0&#39;        });         ^   在../../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:247:13:注意:模板静态类型名称QtPrivate :: QEnableIf&lt;((int)(QtPrivate :: FunctionPointer :: ArgumentCount)&gt ; = 0),QMetaObject :: Connection&gt; :: 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:注意:模板参数扣除/替换失败:   ../_test_gccLambda/main.cpp:替换&#39;模板静态类型名称QtPrivate :: QEnableIf&lt;((int)(QtPrivate :: FunctionPointer :: ArgumentCount)&gt; = 0),QMetaObject :: Connection&gt; ::键入QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1,Func2)[with Func1 = void(QTimer :: )(QTimer :: QPrivateSignal); Func2 = main(int,char *):: __ lambda0]&#39;:   ../_test_gccLambda/main.cpp:14:6:从这里要求   ../_test_gccLambda/main.cpp:14:6:错误:模板参数为&#39;模板静态类型名称QtPrivate :: QEnableIf&lt;((int)(QtPrivate :: FunctionPointer :: ArgumentCount)&gt; = 0),QMetaObject :: Connection&gt; :: Type QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1,Func2)&#39;使用本地类型&#39; main(int,char **):: __ lambda0&#39;        });         ^   ../_test_gccLambda/main.cpp:14:6:错误:尝试实例化&#39;模板静态类型名称QtPrivate :: QEnableIf&lt;((int)(QtPrivate :: FunctionPointer :: ArgumentCount)&gt; = 0),QMetaObject :: Connection&gt; :: Type QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1,Func2)&#39;   在../../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:注意:模板静态类型名称QtPrivate :: QEnableIf&lt;(((int)(QtPrivate :: FunctionPointer :: ArgumentCount) &gt; = 0)&amp;&amp;(!QtPrivate :: FunctionPointer :: IsPointerToMemberFunction)),QMetaObject :: Connection&gt; :: 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:注意:模板参数扣除/替换失败:   ../_test_gccLambda/main.cpp:14:6:注意:无法转换&#39; main(int,char **):: __ lambda0 {}&#39; (键入&#39; main(int,char **):: __ lambda0&#39;)键入&#39; const QObject *&#39;        });         ^   在../../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:287:13:注意:模板静态类型名称QtPrivate :: QEnableIf&lt;(QtPrivate :: FunctionPointer :: ArgumentCount ==( - 1) ),QMetaObject :: Connection&gt; :: 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:注意:模板参数扣除/替换失败:   ../_test_gccLambda/main.cpp:替换&#39;模板静态类型名称QtPrivate :: QEnableIf&lt;(QtPrivate :: FunctionPointer :: ArgumentCount ==( - 1)),QMetaObject :: Connection&gt; :: Type QObject: :connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1,Func2)[with Func1 = void(QTimer :: )(QTimer :: QPrivateSignal); Func2 = main(int,char *):: __ lambda0]&#39;:   ../_test_gccLambda/main.cpp:14:6:从这里要求   ../_test_gccLambda/main.cpp:14:6:错误:模板参数为&#39;模板静态类型名称QtPrivate :: QEnableIf&lt;(QtPrivate :: FunctionPointer :: ArgumentCount ==( - 1)),QMetaObject :: Connection&gt ; ::类型QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1,Func2)&#39;使用本地类型&#39; main(int,char **):: __ lambda0&#39;        });         ^   ../_test_gccLambda/main.cpp:14:6:错误:尝试实例化&#39;模板静态类型名称QtPrivate :: QEnableIf&lt;(QtPrivate :: FunctionPointer :: ArgumentCount ==( - 1)),QMetaObject :: Connection&gt ; ::类型QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1,Func2)&#39;   在../../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:注意:模板静态类型名称QtPrivate :: QEnableIf&lt;(QtPrivate :: FunctionPointer :: ArgumentCount ==( - 1) ),QMetaObject :: Connection&gt; :: 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:注意:模板参数扣除/替换失败:   ../_test_gccLambda/main.cpp:14:6:注意:无法转换&#39; main(int,char **):: __ lambda0 {}&#39; (键入&#39; main(int,char **):: __ lambda0&#39;)键入&#39; const QObject *&#39;        });         ^   make: * [main.o]错误1   16:53:24:过程&#34; / usr / bin / make&#34;退出代码2。   构建/部署项目_test_gccLambda时出错(工具包:Desktop Qt 5.2.0 GCC 64bit)   执行步骤&#39;制作&#39;   16:53:24:经过的时间:00:02。

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

在评论中回答,使用-std = c ++ 11让它用gcc编译