Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to initialize QMultiMap with default values in a namespace
Forum Updated to NodeBB v4.3 + New Features

How to initialize QMultiMap with default values in a namespace

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lepepper
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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).

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lepepper
        wrote on last edited by
        #3

        Thanks for the quick reply.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved