3

I want to initialize a QHash<...> inside a class. There is no problem, if the code is compiled with gcc on linux. But if I use MSVC12, I get the following error:

C2661: QHash<...>::QHash:no overloaded function takes X parameters

Minimal example:

testclass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QHash>
#include <QString>

class TestClass
{
public:
    TestClass();

    QHash<QString, QString> myHash = {{"Hi", "Hello"},
                                      {"test", "Test"}};
};

#endif // TESTCLASS_H

testclass.cpp

#include "testclass.h"

TestClass::TestClass()
{

}

main.cpp

#include <QCoreApplication>
#include <QDebug>

#include "testclass.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    TestClass test;
    qDebug() << test.myHash;

    return a.exec();
}

untitled.pro

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

DEFINES += Q_COMPILER_INITIALIZER_LISTS

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += main.cpp \
    testclass.cpp

HEADERS += \
    testclass.h

Do somebody of you know why MSVC throws this compiler error and how to avoid that?

NelDav
  • 785
  • 2
  • 11
  • 30
  • Does it compile without the `const`? – juanchopanza Jan 21 '20 at 12:34
  • Are you sure that you have the same Qt versions and similar compiler options? – 0xbaadf00d Jan 21 '20 at 12:35
  • Oh that is interesting, it is not the const. If I remove the const, the error is the same. My suggestion ist, that the problem is the initialization inside the class. I will change the heading of my question. – NelDav Jan 21 '20 at 12:37
  • @0xbaadf00d Yes I am sure, that I am using the same Qt versions for g++ and MSVC. The Makefile is generated out of the .pro file. So I am pretty sure, that the compiler options are similar. – NelDav Jan 21 '20 at 12:53
  • Can you use Qt Creator in Windows to try and compile it. – 0xbaadf00d Jan 21 '20 at 13:00
  • Yes I am using Qt Creator on Windows and Linux. – NelDav Jan 21 '20 at 13:01
  • Oh, pardon my old knowledge, doesn't Qt Creator come with mingw anymore? That's what I tried to mean, compile it with mingw from Qt Creator. But I guess that might not be easy anymore. – 0xbaadf00d Jan 21 '20 at 13:02
  • I think you are right, Qt Creator comes with mingw. I tried the minimal example with mingw and it is working. But that is not helping me, because the project where I want to initialize the `QHash` is not compiling with mingw. – NelDav Jan 21 '20 at 13:06

1 Answers1

1

Try this code; if it doesn't work, your compiler doesn't support C++11 or you didn't specify the correct compiler flag to enable it.

#include <vector>
#include <iostream>

const std::vector<int> numbers = {1, 2, 3};

int main()
{
    for(auto num: numbers)
    {
        std::cout << num;
    }
}

The compiler flag might be -std=c++11 But i'm not sure about MSVC12

EDIT

I don't have access to MSVC12 can you try these? I'm thinking that it doesn't fully support C++11

Test 1

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <QHash>
#include <QString>

QHash<QString, QString> const myHash = {{"Hi", "Hello"},
                                        {"test", "Test"}};

class TestClass
{
public:
    TestClass();
};

#endif // TESTCLASS_H

Test 2

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <vector>


class TestClass
{
public:
    TestClass();

    const std::vector<int> numbers = {1, 2, 3};
};

#endif // TESTCLASS_H

Edit

Since Test 1 works and Test 2 doesn't, there is indeed something odd about MSVC12 support of C++11.

C++11 states that you should be allowed to initialize your class member variables in the header file, but it appears that MSVC doesn't support this. At least for initializer lists.

In your original question the QHash was const, now I'm not sure what you wanted to achieve with this, but I'm fairly certain that you either need more const or you could have the QHash as a constant in the X.cpp file you have. Maybe the strings inside the QHash should be const as well?

But that is outside the scope of this question, maybe have a think about that(?)

0xbaadf00d
  • 2,535
  • 2
  • 24
  • 46
  • I tried your code and it works. I think that means, that MSVC is supporting C++11. Am I right? – NelDav Jan 21 '20 at 12:51
  • Yeah, that leaves only the Qt version. – 0xbaadf00d Jan 21 '20 at 12:58
  • Qt version is 5.4.2. I know that is old, but it is the same for g++ and MSVC. – NelDav Jan 21 '20 at 13:00
  • @0xbaadf00d you are dead on the money. mvsc support of c++11 was pretty strange til at least msvc15. https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019 – UmNyobe Jan 22 '20 at 09:23
  • Test 1: works fine. Ofcourse I need to change `qDebug() << test.myHash;` to `qDebug() << myHash;` But after that it worked. – NelDav Jan 23 '20 at 12:41
  • I think you both are right. MSVC12 does not support c++11 well. – NelDav Jan 23 '20 at 12:46
  • Don't worry. I want to wait a few more days, maybe someone else comes and writes a answer, that makes it work with MSVC12. If no one comes, you will get the accept. – NelDav Jan 24 '20 at 09:08