如何初始化 std::map 一次以便它可以被类的所有对象使用?

发布于 2024-08-17 20:49:16 字数 975 浏览 3 评论 0原文

我有一个枚举 StackIndex 定义如下:

typedef enum 
{
    DECK,
    HAND,
    CASCADE1,
    ...
    NO_SUCH_STACK
} StackIndex;

我创建了一个名为 MoveSequence 的类,它是一组 std::deque 形式的元组的包装器 <代码>

class MoveSequence
{
    public:
        void AddMove( const tpl_move & move ){ _m_deque.push_back( move ); }
        void Print();
    protected:
    deque<tpl_move> _m_deque;
};

我想我可以创建 MoveSequence 类的静态 std::map 成员,它将把 StackIndex 转换为 std: :string,供 Print() 函数使用。但是当我尝试时,我得到了错误:

"error C2864: 'MoveSequence::m' : only static const integral data members can be initialized within a class"

如果无法创建 std::map 作为静态成员,是否有另一种方法来创建将 StackIndex 转换为 a std::string 可用于打印 MoveSequence 对象?

谢谢

蜜蜂乐队。

I have an enum StackIndex defined as follows:

typedef enum 
{
    DECK,
    HAND,
    CASCADE1,
    ...
    NO_SUCH_STACK
} StackIndex;

I have created a class called MoveSequence, which is a wrapper for a std::deque of a bunch of tuples of the form <StackIndex, StackIndex>.

class MoveSequence
{
    public:
        void AddMove( const tpl_move & move ){ _m_deque.push_back( move ); }
        void Print();
    protected:
    deque<tpl_move> _m_deque;
};

I thought I could create a static std::map member of the MoveSequence class, which would translate a StackIndex to a std::string, for use by the Print() function. But when I tried, I got the error:

"error C2864: 'MoveSequence::m' : only static const integral data members can be initialized within a class"

If its not possible to created a std::map as a static member, is there another way to create a std::map that translates a StackIndex to a std::string that can be used to print out MoveSequence objects?

thanks

Beeband.

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

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

发布评论

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

评论(4

野鹿林 2024-08-24 20:49:16

您需要将初始化移动到源文件中:

// header
struct foo
{
    typedef std::map<unsigned, std::string> the_map;
    static const the_map m;
};

// source
const foo::the_map foo::m(...);

无论您想要初始化它。 C++0x 消除了这个限制。

请记住 Boost.Assign 使此很简单:

#include <boost/assign.hpp>
const foo::the_map foo::m = boost::assign::map_list_of(1, "a")(2, "b");

You need to move the initialization into a source file:

// header
struct foo
{
    typedef std::map<unsigned, std::string> the_map;
    static const the_map m;
};

// source
const foo::the_map foo::m(...);

With however you want to initialize it. C++0x removes this restriction.

Keep in mind Boost.Assign makes this quite easy:

#include <boost/assign.hpp>
const foo::the_map foo::m = boost::assign::map_list_of(1, "a")(2, "b");
白馒头 2024-08-24 20:49:16

您可以使 std::map 成为该类的静态成员。你不能做的是在类定义中初始化它。请注意,这是错误告诉您的内容:

错误 C2864:“MoveSequence::m”:只有静态常量整型数据成员才能在类中*初始化*

因此,您希望在标头中包含此内容:

class MoveSequence
{
    static std::map<StackIndex, std::string> _m_whatever;
};

然后在源 (.cpp )文件你想要这个:

std::map<StackIndex, std::string> MoveSequence::_m_whatever( ..constructor args.. );

You can make a std::map a static member of the class. What you can't do is initiliaze it within the class definition. Note that this is what the error is telling you:

error C2864: 'MoveSequence::m' : only static const integral data members can be *initialized* within a class

So, you want to have this in the header:

class MoveSequence
{
    static std::map<StackIndex, std::string> _m_whatever;
};

And then in a source (.cpp) file you want this:

std::map<StackIndex, std::string> MoveSequence::_m_whatever( ..constructor args.. );
雨巷深深 2024-08-24 20:49:16

我不认为你想要一个 std::map (尽管这里的所有其他答案都是关于如何做到这一点的好答案)。听起来您只需要一个静态 C 字符串数组,其中索引是枚举值。

const char* const stacknames[] = 
{
    "deck",
    "hand",
    "cascade1"
};

那么 stacknames[DECK] 就是“deck”,等等。

I don't think you want a std::map (although all of the other answers here are good ones as to how to do that). It sounds like you just want a static C array of strings, where the index is the enum value.

const char* const stacknames[] = 
{
    "deck",
    "hand",
    "cascade1"
};

Then stacknames[DECK] is "deck", etc.

何处潇湘 2024-08-24 20:49:16

正如其他人所建议的,您需要在 C++ 源文件中创建地图的静态实例。在初始化它时,我建议在 MoveSequence 中创建一个静态函数:

class MoveSequence {

   static void InitMap() {
      if ( m_map.size() == 0 ) {
           m_map.insert( std::make_pair( DECK, "deck" ) );
            m_map.insert( std::make_pair( HAND, "hand" ) );
      }
   }
   ...
};

然后您可以从 MoveSequence 的构造函数中调用它。

哦,顺便说一句,枚举上不需要 typedef:

enum StackIndex {
  ...
};

As others have suggested, you need to create a static instance of the map in a C++ source file. When it comes to initialising it, I suggest creating a static function in MoveSequence:

class MoveSequence {

   static void InitMap() {
      if ( m_map.size() == 0 ) {
           m_map.insert( std::make_pair( DECK, "deck" ) );
            m_map.insert( std::make_pair( HAND, "hand" ) );
      }
   }
   ...
};

You can then call this from MoveSequence's constructor.

Oh, and BTW there is no need for the typedef on the enum:

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