Note that there are some explanatory texts on larger screens.

plurals
  1. POconflicting declaration when filling a static std::map class member variable
    primarykey
    data
    text
    <p>I have a class with a static std::map member variable that maps <code>char</code>s to a custom type <code>Terrain</code>. I'm attempting to fill this map in the class's implementation file, but I get several errors. Here's my header file:</p> <pre><code>#ifndef LEVEL_HPP #define LEVEL_HPP #include &lt;bitset&gt; #include &lt;list&gt; #include &lt;map&gt; #include &lt;string&gt; #include &lt;vector&gt; #include "libtcod.hpp" namespace yarl { namespace level { class Terrain { // Member Variables private: std::bitset&lt;5&gt; flags; // Member Functions public: explicit Terrain(const std::string&amp; flg) : flags(flg) {} (...) }; class Level { private: static std::map&lt;char, Terrain&gt; terrainTypes; (...) }; } } #endif </code></pre> <p>and here's my implementation file:</p> <pre><code>#include &lt;bitset&gt; #include &lt;list&gt; #include &lt;map&gt; #include &lt;string&gt; #include &lt;vector&gt; #include "Level.hpp" #include "libtcod.hpp" using namespace std; namespace yarl { namespace level { /* fill Level::terrainTypes */ map&lt;char,Terrain&gt; Level::terrainTypes['.'] = Terrain("00001"); // clear map&lt;char,Terrain&gt; Level::terrainTypes[','] = Terrain("00001"); // clear map&lt;char,Terrain&gt; Level::terrainTypes['\''] = Terrain("00001"); // clear map&lt;char,Terrain&gt; Level::terrainTypes['`'] = Terrain("00001"); // clear map&lt;char,Terrain&gt; Level::terrainTypes[178] = Terrain("11111"); // wall (...) } } </code></pre> <p>I'm using g++, and the errors I get are</p> <p>src/Level.cpp:15: error: conflicting declaration ‘std::map, std::allocator > > yarl::level::Level::terrainTypes [46]’<br> src/Level.hpp:104: error: ‘yarl::level::Level::terrainTypes’ has a previous declaration as ‘std::map, std::allocator > > yarl::level::Level::terrainTypes’<br> src/Level.cpp:15: error: declaration of ‘std::map, std::allocator > > yarl::level::Level::terrainTypes’ outside of class is not definition<br> src/Level.cpp:15: error: conversion from ‘yarl::level::Terrain’ to non-scalar type ‘std::map, std::allocator > >’ requested<br> src/Level.cpp:15: error: ‘yarl::level::Level::terrainTypes’ cannot be initialized by a non-constant expression when being declared</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload