1

I need to create a QVectorIterator to iterate a QVector of QStrings as follows:

#include <QString>
#include <QVectorIterator>

#include <QLabel>
#include <QTimer>

class Dice : public QLabel
{
    Q_OBJECT

    private:
        QVector<QString> dice_faces;
        QVectorIterator<QString> it( dice_faces );
        QTimer *timer;
    ...

But I get this error, and don't understand what is wrong, or did QVectorIterator can't iterate over QString vector?

Dice.h:16: error: 'dice_faces' is not a type
     QVectorIterator<QString> i( dice_faces );
                                 ^

1 Answer 1

2

You need to initialize the iterator in the initializer list in the constructor

#include <QString>
#include <QVectorIterator>

#include <QLabel>
#include <QTimer>

class Dice : public QLabel
{
    Q_OBJECT

    public:
        Dice(QObject *parent);

    private:
        QVector<QString> dice_faces;
        QVectorIterator<QString> it( dice_faces );
        QTimer *timer;
    // ...

dice.cpp

// ...

Dice::Dice(QObject *parent)
  : QLabel(parent),
    it(dice_faces)
{
}
Sign up to request clarification or add additional context in comments.

2 Comments

I Knew it will be a stupid question, I'm under pression..., thanks anyway
You're welcome. I still don't think this is a beautiful solution. Consider declaring the iterator in the local scope where you iterate only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.