QVector :: содержит ошибки с проверками на константы - PullRequest
0 голосов
/ 10 апреля 2020

Среда, как указано в тегах:

G CC 64bit, Qt 5.12

У меня есть следующий пример кода:

// test.h

#include <QVector>

class Test 
{
    Test();

    // Same results with QSet
    const QVector<int> things = {
        BANANA,
        RASPBERRY,
        APPLE,
        -2500,
    };

    const int BANANA = -1000;
    const int RASPBERRY = -2000;
    const int APPLE = -3000;
};
// test.cpp

#include <QDebug>
#include "test.h"

Test::Test()
{
    qDebug() << things.contains(APPLE); // false, expected true
    qDebug() << things.contains(-3000); // false, expected true
    qDebug() << things.contains(-2500); // true, expected true
}

Я не понимаю, сделал ли я что-то неправильно в определениях или я столкнулся с ошибкой в ​​Qt.

1 Ответ

0 голосов
/ 10 апреля 2020

Кажется, я пытался использовать постоянные переменные, которые еще не определены

// test.h

#include <QVector>

class Test 
{
    Test();

    // Moved them above QVector
    const int BANANA = -1000;
    const int RASPBERRY = -2000;
    const int APPLE = -3000;

    const QVector<int> things = {
        BANANA,
        RASPBERRY,
        APPLE,
        -2500,
    };
};
// test.cpp

#include <QDebug>
#include "test.h"

Test::Test()
{
    // Now the tests pass as expected
    qDebug() << things.contains(APPLE); // true, expected true
    qDebug() << things.contains(-3000); // true, expected true
    qDebug() << things.contains(-2500); // true, expected true
}

Не ошибка в QT!

...