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 820867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:32:50+00:00 2026-05-15T02:32:50+00:00

I have a class with a static std::map member variable that maps char s

  • 0

I have a class with a static std::map member variable that maps chars to a custom type Terrain. I’m attempting to fill this map in the class’s implementation file, but I get several errors. Here’s my header file:

#ifndef LEVEL_HPP
#define LEVEL_HPP

#include <bitset>
#include <list>
#include <map>
#include <string>
#include <vector>
#include "libtcod.hpp"

namespace yarl
{
    namespace level
    {
        class Terrain
        {
        // Member Variables
            private:
               std::bitset<5> flags;

        // Member Functions
            public:
                explicit Terrain(const std::string& flg)
                : flags(flg) {}

            (...)
        };



        class Level
        {
            private:
                static std::map<char, Terrain> terrainTypes;

            (...)
        };
    }
}

#endif 

and here’s my implementation file:

#include <bitset>
#include <list>
#include <map>
#include <string>
#include <vector>
#include "Level.hpp"
#include "libtcod.hpp"
using namespace std;

namespace yarl
{
    namespace level
    {
        /* fill Level::terrainTypes */
        map<char,Terrain> Level::terrainTypes['.'] = Terrain("00001");  // clear
        map<char,Terrain> Level::terrainTypes[','] = Terrain("00001");  // clear 
        map<char,Terrain> Level::terrainTypes['\''] = Terrain("00001"); // clear
        map<char,Terrain> Level::terrainTypes['`'] = Terrain("00001");  // clear
        map<char,Terrain> Level::terrainTypes[178] = Terrain("11111");  // wall

        (...)
    }
}

I’m using g++, and the errors I get are

src/Level.cpp:15: error: conflicting declaration ‘std::map, std::allocator > > yarl::level::Level::terrainTypes [46]’
src/Level.hpp:104: error: ‘yarl::level::Level::terrainTypes’ has a previous declaration as ‘std::map, std::allocator > > yarl::level::Level::terrainTypes’
src/Level.cpp:15: error: declaration of ‘std::map, std::allocator > > yarl::level::Level::terrainTypes’ outside of class is not definition
src/Level.cpp:15: error: conversion from ‘yarl::level::Terrain’ to non-scalar type ‘std::map, std::allocator > >’ requested
src/Level.cpp:15: error: ‘yarl::level::Level::terrainTypes’ cannot be initialized by a non-constant expression when being declared

I get a set of these for each map assignment line in the implementation file. Anyone see what I’m doing wrong? Thanks for your help.

  • 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-15T02:32:50+00:00Added an answer on May 15, 2026 at 2:32 am

    You can initialize static members outside of functions, but you can’t perform arbitrary operations.

    You could use a function to initialize the members:

    namespace {
        std::map<char, Terrain> initTerrainTypes() {
            std::map<char, Terrain> m;
            m['.'] = Terrain("00001"); 
            // ...
            return m;
        }
    }
    
    map<char,Terrain> Level::terrainTypes = initTerrainTypes();
    

    Or you could use initialization utilities like Boost.Assign:

    map<char,Terrain> Level::terrainTypes = boost::assign::map_list_of
       ('.', Terrain("00001"))
       // ...
       (178, Terrain("11111"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying
i have a full static class, using a std::map thats the simplified case .h
If I have a class called Test :: class Test { static std::vector<int> staticVector;
I have a static std::vector in a class. When I use Microsoft's memory leak
I have class A such that: class A { static int i; A(); f1();
I have a class with static method which has a local static variable. I
Let's say I have a class (not a static class), A , that in
I have a static class that I use as my Data Utils for my
I have written a class (AbcdBase) which holds several static objects, these include maps
For training, I'm trying to write a class like std::string. I have read that

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.