0

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.

3
  • 1
    converting from char to const std::string* is the least of your worries Commented Feb 11, 2011 at 7:15
  • Suit(void) is not a constructor. You declare a constructor by declaring a function with no return type and the same name as the class. Commented Feb 11, 2011 at 7:17
  • Also, strings should not be used as a universal variable type. Enumerations are recommended here. Commented Feb 11, 2011 at 7:20

2 Answers 2

4

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", ...};
Sign up to request clarification or add additional context in comments.

Comments

0

An array of chars has type const char[]. What you have, const char*[] is an array of pointers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.