English 中文(简体)
How to initialise a std::map once so that it can be used by all objects of a class?
原标题:

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.

最佳回答

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.. );
问题回答

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");

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.

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 {
  ...
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签