Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6167997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:36:36+00:00 2026-05-23T22:36:36+00:00

I have a few static unordered_maps in my application that I would like to

  • 0

I have a few static unordered_maps in my application that I would like to be able to initialize on start up. They are not part of a class. In my initial setup, the header and source file were as follows:

namespace Game
{
    namespace Elements
    {

        enum Element
        {
            Air = 1,
            Dark = 2,
            Earth = 4,
            Elementless = 8,
            Fire = 16,
            Ice = 32,
            Light = 64,
            SpaceTime = 128,
            Thunder = 256,
            Water = 512,
        };
        static boost::unordered_map<Element, std::string> ElementNameMap;
        static boost::unordered_map<std::string, Element> NameElementMap
        }
}

And the source file:

#include "Elements.h"

using namespace std;
using namespace Game::Elements;

boost::unordered_map<Element, std::string> ElementNameMap = boost::assign::map_list_of
    (Element::Air, string("Air"))
    (Element::Dark, string("Dark"))
    (Element::Earth, string("Earth"))
    (Element::Elementless, string("Elementless"))
    (Element::Fire, string("Fire"))
    (Element::Ice, string("Ice"))
    (Element::Light, string("Light"))
    (Element::SpaceTime, string("SpaceTime"))
    (Element::Thunder, string("Thunder"))
    (Element::Water, string("Water"))
    ;

boost::unordered_map<std::string, Element> NameElementMap = boost::assign::map_list_of
    (string("Air"), Element::Air)
    (string("Dark"), Element::Dark)
    (string("Earth"), Element::Earth)
    (string("Elementless"), Element::Elementless)
    (string("Fire"), Element::Fire)
    (string("Ice"), Element::Ice)
    (string("Light"), Element::Light)
    (string("SpaceTime"), Element::SpaceTime)
    (string("Thunder"), Element::Thunder)
    (string("Water"), Element::Water)
    ;

However, whenever I tried doing this then tried to do a lookup on the maps (i.e. Game::Elements::NameElementMap[std::string(“Air”)] ) it always returns an empty string, and the size in that use context is 0.

I tried moving the initialization into the header file (i.e. in the header file putting

static boost::unordered_map<Element, std::string> ElementNameMap = boost::assign::map_list_of
    (Element::Air, string("Air"))
    (Element::Dark, string("Dark"))
    (Element::Earth, string("Earth"))
    (Element::Elementless, string("Elementless"))
    (Element::Fire, string("Fire"))
    (Element::Ice, string("Ice"))
    (Element::Light, string("Light"))
    (Element::SpaceTime, string("SpaceTime"))
    (Element::Thunder, string("Thunder"))
    (Element::Water, string("Water"))
    ;
static boost::unordered_map<std::string, Element> NameElementMap = boost::assign::map_list_of
    (string("Air"), Element::Air)
    (string("Dark"), Element::Dark)
    (string("Earth"), Element::Earth)
    (string("Elementless"), Element::Elementless)
    (string("Fire"), Element::Fire)
    (string("Ice"), Element::Ice)
    (string("Light"), Element::Light)
    (string("SpaceTime"), Element::SpaceTime)
    (string("Thunder"), Element::Thunder)
    (string("Water"), Element::Water)
    ;

but the compiler then complained about having no default constructor. What am I doing wrong?

thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:36:37+00:00Added an answer on May 23, 2026 at 10:36 pm

    You have to put the definitions in the correct namespace. Otherwise you are defining two new maps in the global namespace, instead of the ones in Game::Elements:

    namespace Game {
    namespace Elements {
    
    boost::unordered_map<Element, std::string> ElementNameMap = boost::assign::map_list_of
        (Air, string("Air"))
        (Dark, string("Dark"))
    ...
    
    }
    }
    

    (Also enums don’t create a new namespace, so it’s just Air, not Element::Air.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface based class that I want to have a few static
I have a few hundred static HTML files that need to be processed. They
I have few tables like Country,state city which has static data. User do not
I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{
I have few Question for which I am not able to get a proper
I have few global methods declared in public class in my ASP.NET web application.
I have a few models that contain static data, and I'm trying to cache
I have a few static household ads in an UIImageView that just display some
I have a few general use functions that do not really make sense in
I have few methods that returns different Generic Lists. Exists in .net any class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.