填充静态 std::map 类成员变量时发生冲突声明

发布于 2024-09-03 08:35:20 字数 2227 浏览 1 评论 0原文

我有一个带有静态 std::map 成员变量的类,它将 char 映射到自定义类型 Terrain。我试图将此映射填充到类的实现文件中,但出现了几个错误。这是我的头文件:

#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 

这是我的实现文件:

#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

        (...)
    }
}

我正在使用 g++,我得到的错误是

src/Level.cpp:15: error:conflicting statements 'std::map, std::allocator > > yarl::level::Level::terrainTypes [46]'
src/Level.hpp:104: 错误:'yarl::level::Level::terrainTypes' 之前的声明为 'std::map, std::allocator > > yarl::level::Level::terrainTypes'
src/Level.cpp:15: 错误: 声明 'std::map, std::allocator > > yarl::level::Level::terrainTypes' 在类之外不是定义
src/Level.cpp:15: 错误:从 'yarl::level::Terrain' 转换为非标量类型 'std::map, std::allocator > >'已请求
src/Level.cpp:15: error: 'yarl::level::Level::terrainTypes' 在声明时无法由非常量表达式初始化

我为实现文件中的每个地图分配行获取一组这些表达式。有人看到我做错了什么吗?感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2024-09-10 08:35:20

您可以在函数外部初始化静态成员,但不能执行任意操作。

您可以使用函数来初始化成员:

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

map<char,Terrain> Level::terrainTypes = initTerrainTypes();

或者您可以使用初始化实用程序,例如 Boost.Assign

map<char,Terrain> Level::terrainTypes = boost::assign::map_list_of
   ('.', Terrain("00001"))
   // ...
   (178, Terrain("11111"));

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"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文