How to initialize static const char array for ASCII codes [C++]
I want to initialize a static const char array with ASCII codes in a constructor, here's my code:
class Card
{
public:
Suit(void)
{
static const char *Suit[4] = {0x03, 0x04, 0x05, 0x06}; // here's the problem
static const string *Rank[ 13 ] = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'}; // and here.
}
However i got a whole lot of errors stating that
'initializing'开发者_运维技巧 : cannot convert from 'char' to 'const std::string *'
'initializing' : cannot convert from 'int' to 'const std::string *'
please help me! Thank you so much.
You are initializing just one array of characters, so you want:
static const char Suit[] = {0x03, 0x04, 0x05, 0x06};
static const char Rank[] = {'A', '2', ...};
The forms that you are using are declaring arrays of strings and then initializing them with single strings. If you do want Rank
to be an array of strings, the initializers need to be in double quotes:
static const char* Rank[] = {"A", "2", ...};
or:
static const std::string Rank[] = {"A", "2", ...};
An array of chars has type const char[]
. What you have, const char*[]
is an array of pointers.
精彩评论