How to initialize QMultiMap with default values in a namespace



  • I have created a new header file ( ,h) just for creating/declaring new data types.
    this does not have a class,just name space. so no .cpp file is created

    I tried doing something like this,it gives error,that it cannot be initialized like this,we need a constructor /destructor
    @
    #include <QMultiMap>
    namespace charset
    {
    QMultiMap<QChar, QString> charset =
    {
    ('A', "a"),
    ('B', "b")
    };
    }
    @

    The above did not work,
    so I tried doing this

    @
    #include <QMultiMap>
    namespace charset
    {
    QMultiMap<QChar, QString> charset;

    charset.insert ('A', "a");
    charset.insert ('B', "b");

    }
    @

    This ask for a constructor and destructor, Tried making it static and defining it outside,did not work.
    Any ideas how to do this



  • I moved this to the C++ Gurus forum, as it's more a general C++ question and Qt is only involved by chance.

    Regarding you problem:
    A namespace is more or less a container for symbols, i.e. for constant names, variable names, function names and more of that. You can define your multimap variable there, but you cannot initialize it there. As for any other static variable, this must be done once in a file that generates code. A header file usually is not compiled and therefore does not generate code. You must put your initialization code into a function and implement this in a separate .cpp file (similar to a regular C++ class).



  • Thanks for the quick reply.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.