如何初始化 std::map 一次以便它可以被类的所有对象使用?
我有一个枚举 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将初始化移动到源文件中:
无论您想要初始化它。 C++0x 消除了这个限制。
请记住 Boost.Assign 使此很简单:
You need to move the initialization into a source file:
With however you want to initialize it. C++0x removes this restriction.
Keep in mind Boost.Assign makes this quite easy:
您可以使 std::map 成为该类的静态成员。你不能做的是在类定义中初始化它。请注意,这是错误告诉您的内容:
因此,您希望在标头中包含此内容:
然后在源 (.cpp )文件你想要这个:
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:
So, you want to have this in the header:
And then in a source (.cpp) file you want this:
我不认为你想要一个 std::map (尽管这里的所有其他答案都是关于如何做到这一点的好答案)。听起来您只需要一个静态 C 字符串数组,其中索引是枚举值。
那么 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.
Then stacknames[DECK] is "deck", etc.
正如其他人所建议的,您需要在 C++ 源文件中创建地图的静态实例。在初始化它时,我建议在 MoveSequence 中创建一个静态函数:
然后您可以从 MoveSequence 的构造函数中调用它。
哦,顺便说一句,枚举上不需要 typedef:
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:
You can then call this from MoveSequence's constructor.
Oh, and BTW there is no need for the typedef on the enum: