首页 文章

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

提问于
浏览
0

本代码编译没有任何问题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:警告:lambda表达式仅适用于-std = c 11或-std = gnu 11 [默认启用]}); ^ ../_test_gccLambda/main.cpp:14:6:错误:没有用于调用'QObject :: connect(QTimer *,void(QTimer ::)(QTimer :: QPrivateSignal),main(int,char *)的匹配函数):: __ lambda0)'}); ^ ../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,from ../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&,const QObject *,const QMetaMethod&,Qt: :ConnectionType)静态QMetaObject ::连接连接(const QObject * sender,const QMetaMethod&signal,^ ../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:201:36:注意:候选人期望5个论者ts,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 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:注意:模板静态QMetaObject ::连接QObject :: connect(const typename QtPrivate :: FunctionPointer :: Object *,Func1,const typename QtPrivate :: FunctionPointer :: Object *,Func2,Qt :: ConnectionType)静态内联QMetaObject ::连接连接(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 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,来自../test_gccLambda/main.cpp:1:../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:247:13:注意:模板静态类型名称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:注意:模板参数演绎/ substitution failed:../ test_gccLambda / main.cpp:替换'template static typename 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:错误:模板参数为' template static typename 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,from ../test_gccLambda/main.cpp:1:../../Qt5/5.2 . 0 / gcc_64 / include / QtCore / qobject.h:256:13:注意:模板静态类型名称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:模板参数推导/替换失败:../ test_gccLambda / main.cpp:14:6:注意:无法转换'main(int,char **):: __ lambda0 {}'(类型'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,来自../test_gccLambda/main.cpp:1:../../Qt5/5.2.0/gcc_64/include/QtCore/qobject.h:287:13:注意:模板静态类型名称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:注意:模板参数扣除/替换失败:../ test_gccLambda / main.cpp:代替'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:从这里需要../_test_gccLambda/main.cpp:14:6:错误:模板参数为' template static typename QtPrivate :: QEnableIf <(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,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:注意:模板静态类型名称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:注意:模板参数扣除/替换fai led:../ test_gccLambda / main.cpp:14:6:注意:无法转换'main(int,char **):: __ lambda0 {}'(类型'main(int,char **):: __ lambda0')输入'const QObject '}); ^ make: [main.o]错误1 16:53:24:进程“/ usr / bin / make”退出代码2.构建/部署项目时出错_test_gccLambda(kit:Desktop Qt 5.2.0 GCC 64bit)执行步骤'Make'16:53:24:经过时间:00:02 .

问题出在哪儿?

1 回答

  • 0

    如评论中所述,使用-std = c 11将其与gcc进行编译

相关问题