0

当我编译这段代码

QVector<QString> taskTitle({"Movies which are directed by Steven Spilberg",
                           "All those who have reviewed Gone whith the wind",
                           "Summation of Gone with the wind scores",
                           "All years which has a movie whith 5 or 4 scores increasingly sortd"});

编译器给了我:

error: no matching function for call to
   'QVector<QString>::QVector(<brace-enclosed initializer list>)'

我也用过:

QVector<QString> taskTitle={"Movies which are directed by Steven Spilberg",
                           "All those who have reviewed Gone whith the wind",
                           "Summation of Gone with the wind scores",
                           "All years which has a movie whith 5 or 4 scores increasingly sortd"};

然后再次:

error: in C++98 'taskTitle' must be initialized by constructor, not by '{...}'

我的编译器是 MinGW 并附带 QT 5.0.1 我能做什么?

4

1 回答 1

2

可能的解决方案

我认为要在 MinGW 中启用 C++11 功能,您应该设置适当的标志。有QMAKE_CXXFLAGS设置编译器选项qmake。所以你的.pro文件应该是这样的:

QT       += core
QT       -= gui
QT       += sql

# or c++0x
QMAKE_CXXFLAGS += -std=c++11 

TARGET = untitled
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app

SOURCES += main.cpp

添加细节

Qt 也有Q_COMPILER_INITIALIZER_LISTS宏来确定集合是否提供初始化列表构造函数。例如在QVector

#ifdef Q_COMPILER_INITIALIZER_LISTS
#include <initializer_list>
#endif

template <typename T>
class QVector
{
    // ...

#ifdef Q_COMPILER_INITIALIZER_LISTS
     inline QVector(std::initializer_list<T> args);
#endif

    // ...
};

基于这个标志,我们可以创建一个小应用程序来测试使用初始化列表的可行性:

#ifdef Q_COMPILER_INITIALIZER_LISTS
    qDebug("Yes");
#else
    qDebug("No");
#endif

该标志在qcompilerdetection.hQt5 的文件中定义(qglobal.h对于 Qt4)。例如,对于我拥有的编译器(它是 gcc 4.7.2),以下几行将启用 C++11 功能。

#if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) && !defined(Q_CC_CLANG)
#  if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
#    if (__GNUC__ * 100 + __GNUC_MINOR__) >= 404
       /* C++11 features supported in GCC 4.4: */
#      define Q_COMPILER_INITIALIZER_LISTS
#    endif
#  endif
#endif

因此,如果通过设置无法解决问题,QMAKE_CXXFLAGS您可以查看此文件并探索为您的编译器集启用了哪些标志。

于 2013-05-26T08:31:23.543 回答