0

Environment as stated in the tags too:

GCC 64bit, Qt 5.12

I have the following example code:

// 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
}

I don't understand if I've done something wrong in the definitions or I encountered a bug in Qt.

Matteo
  • 1,107
  • 2
  • 27
  • 47
  • 1
    Can't reproduce (Suse Linux, Qt5.14.1, g++ 9.2.1). Please edit your question to provide a [mcve]. – G.M. Apr 10 '20 at 09:55
  • I supposed tags where enough, added explicit information at the beginning – Matteo Apr 10 '20 at 10:01
  • 1
    How are you verifying the result of `contains`? Did you make sure your program compiles and you're not running an older version of the binary? – Botje Apr 10 '20 at 11:06
  • I did a clean run each time to be sure to have the correct results, every iteration I got the "wrong" results – Matteo Apr 10 '20 at 12:23
  • Fixed the example, thanks to @Amish – Matteo Apr 10 '20 at 14:02

1 Answers1

0

Seems I was trying to use constant variables not defined yet

// 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
}

Not a bug in QT!

Matteo
  • 1,107
  • 2
  • 27
  • 47