【问题标题】:Extended initializer list for Qvector<qstring>Qvector<qstring> 的扩展初始化列表
【发布时间】:2013-05-21 08:30:26
【问题描述】:

当我编译这段代码时

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 我能做什么?

【问题讨论】:

  • 您是否有 C++11 兼容的编译器并设置了适当的编译标志 (-std=c++11)?你有什么版本的Qt?请更新问题并添加有关您的构建的其他信息。
  • 我的编译器是 MinGW 并附带 QT 5.0.1
  • 如果您使用qmake,您的.pro 文件中是否有QMAKE_CXXFLAGS += -std=c++11(或类似名称)?或者,如果您不能将构建脚本附加到问题中?
  • 没有所有的东西:QT += core QT -= gui QT +=sql TARGET = Sqlite CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp跨度>
  • @MrHsCE 照fasked所说的去做,这就是你的解决方案。

标签: qt vector initializer-list


【解决方案1】:

可能的解决方案

我认为要在 MinGW 中启用 C++11 功能,您应该设置适当的标志。有QMAKE_CXXFLAGSqmake 设置编译器选项。所以你的.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

此标志在 Qt5 的 qcompilerdetection.h 文件中定义(Qt4 的 qglobal.h)。例如,对于我拥有的编译器(它是 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 无法解决问题,您可以查看此文件并探索为您的编译器集启用了哪些标志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2012-11-13
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多