initializing static array of strings

I need to include static arrays of strings in my class

Page 1 of 1

2 Replies - 5466 Views - Last Post: 19 February 2010 - 03:45 AM Rate Topic: -----

#1 Ironsheik1089  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-June 09

initializing static array of strings

Posted 18 February 2010 - 11:01 PM

I am having some trouble figuring out the static arrays that need to be included in my class. my class should contain the following:

Data members face and suit of type int

constructor that receives two ints representing the face and suit of cards and uses them to initialize the data members

two static arrays of strings representing the face and suits
faceNames = {Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King}
suitNames = {Clubs, Diamonds, Hearts, Spades}

a toString function that returns the card as a string in the form of "Face of suit"

I do have some code written. I am not sure i I am doing the arrays right. Could anyone please take a look and give suggestions.

this is my header file

#include <iostream>
#include <string>
using namespace std;

Class Card
{
public:
    static const int face = 13;
    static const int suit = 4;   
    Card(int, int); // constructor that receives two int values representing # of face 
                    // values and suit values and initializes the data members 
    string toString(); // return the card as a string in the form "Face of Suit"
private:
    string faceNames[face]; // array of card face values 
    string suitNames[suit]; // array of card suits  
};

// initialize Card arrays
const string Card::faceNames[face] ="Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
const string Card::[suit] = {"Clubs", "Diamonds", "Hearts", "Spades"};

// end Class Card



Is This A Good Question/Topic? 0
  • +

Replies To: initializing static array of strings

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: initializing static array of strings

Posted 19 February 2010 - 01:40 AM

Hello Ironsheik,

Thanks for posting your code. I have added a couple of comments in there about a couple of things for you to look at.

#include <iostream>
#include <string>
using namespace std;

Class Card
{
public:
    static const int face = 13;
    static const int suit = 4;   
    Card(int, int); // constructor that receives two int values representing # of face 
                    // values and suit values and initializes the data members 
    string toString(); // return the card as a string in the form "Face of Suit"
private:
    string faceNames[face]; // array of card face values 
    string suitNames[suit]; // array of card suits  
}; // your class ends here... 

// initialize Card arrays
/* becuase they are private, they cannot
   be accessed let alone initialised, and
   you have changed from a variable to constant.
*/
const string Card::faceNames[face] ="Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
const string Card::[suit] = {"Clubs", "Diamonds", "Hearts", "Spades"};

// end Class Card


[/quote]

To actually initialise an array you have a few choices.

Try using enums, or set up an improved class design.
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: initializing static array of strings

Posted 19 February 2010 - 03:45 AM

Your code is suffering from a whole bunch of errors:

- you haven't declared either of your arrays as static within your class definition
- you've missed out the name of one of your arrays when trying to initialise
- you've got a const/non-const mismatch.
- You're missing an opening curly bracket { on the initialisation of faceNames

Did you intend to do this?
private:
    static string const faceNames[face]; // array of card face values 
    static string const suitNames[suit]; // array of card suits  
};

// initialize Card arrays
const string Card::faceNames[face] = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
const string Card::suitNames[suit] = {"Clubs", "Diamonds", "Hearts", "Spades"}; 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1