feedc0de
feedc0de

Reputation: 3796

Cannot initialize QQueue with initializer list

A QList<T> lets me initialize it with some entries using a initializer list.

Doing the same with a Queue<T> gives me a compiler error.

Example code does not compile:

QList<qreal> someNumbers { 0.0, 0.1 };
QQueue<qreal> someOtherNumbers { 0.0, 0.1 };

Compiler output:

error: no matching function for call to 'QQueue<double>::QQueue(<brace-enclosed initializer list>)'
     QQueue<qreal> someOtherNumbers { 0.0, 0.1 };
                                               ^
qqueue.h:49:7: note: candidate: QQueue<double>::QQueue()
 class QQueue : public QList<T>
       ^
qqueue.h:49:7: note:   candidate expects 0 arguments, 2 provided
qqueue.h:49:7: note: candidate: QQueue<double>::QQueue(const QQueue<double>&)
qqueue.h:49:7: note:   candidate expects 1 argument, 2 provided
qqueue.h:49:7: note: candidate: QQueue<double>::QQueue(QQueue<double>&&)
qqueue.h:49:7: note:   candidate expects 1 argument, 2 provided

Is there some way to initialize a QQueue<T> using an initializer list?

Upvotes: 2

Views: 484

Answers (1)

Gabrielle de Grimouard
Gabrielle de Grimouard

Reputation: 2005

Since QQueue inherit from QList, I found a trick to do that.

QQueue<double> *queue;
QList<double> list = {{0.0, 1.1}};
queue = reinterpret_cast<QQueue<double> *>(&list);
qDebug() << queue->isEmpty();
while (!queue->isEmpty()) {
    qDebug() << queue->dequeue();
}

however, in this example, list it temporary so if you want to use this outside to the function you will have to use new

Upvotes: 2

Related Questions