views:

97

answers:

1

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  A: 

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"));
Georg Fritzsche
The first option fixed the problem. Thanks for your help.
Max