MrHs CE
MrHs CE

Reputation: 3

Extended initializer list for Qvector<qstring>

When I compile this code

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"});

compiler gives me:

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

I also used:

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"};

and again:

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

my compiler is MinGW and came with QT 5.0.1 What can I do?

Upvotes: 0

Views: 3235

Answers (1)

fasked
fasked

Reputation: 3645

Possible solution

I think to enable C++11 features in MinGW you should set the appropriate flag. There is QMAKE_CXXFLAGS to set compiler options for qmake. So your .pro file should look like:

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

Addition details

Also Qt has Q_COMPILER_INITIALIZER_LISTS macro to determine whether collections provide initializer-lists constructors or not. For example in 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

    // ...
};

Based on this flag we can create a small application to test the feasibility of using a initializer list:

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

This flag is defined in qcompilerdetection.h file for Qt5 (qglobal.h for Qt4). For example for compiler I have (it is gcc 4.7.2) the following lines will enable C++11 features.

#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

So if problem isn't solved by setting QMAKE_CXXFLAGS you can see into this file and explore which of flags are enabled for your compiler set.

Upvotes: 2

Related Questions