I have a class with a static std::map member variable that maps char
s 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.