By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,952 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,952 IT Pros & Developers. It's quick & easy.

Initializing 2D array in constructor

P: n/a
I was wondering if anyone can help me with this problem.
#include <iostream>
#include <stdlib.h>
I am having problems compiling the following code. Can someone explain
what is wrong with the constructor?

#include <stdio.h>
#include <string>

using namespace std;

class deckOfCards{
public:
deckOfCards();

private:
char* suit[4][8];
char* face[13][8];
};

deckOfCards::deckOfCards()
{
suit[4][8] = {"Diamonds", "Hearts", "Clubs", "Spades"};
char *face[13][8] = {"Ace", "One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Jack", "Queen", "King"};
}

Nov 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
al******@gmail.com wrote:
I was wondering if anyone can help me with this problem.
#include <iostream>
#include <stdlib.h>
I am having problems compiling the following code. Can someone explain
what is wrong with the constructor?
Several things are wrong. What book are you reading? See www.accu.org
for suggestions and read http://www.parashift.com/c++-faq-lite/.
#include <stdio.h>
#include <string>
What don't you use std::string??

using namespace std;

class deckOfCards{
public:
deckOfCards();

private:
char* suit[4][8];
char* face[13][8];
suit will contain 32 pointers to chars and face will contain 104
pointers to chars. Is that what you want? You probably need to have 4
suit names and 13 face names, each of them having a maximum of 8
letters:

char suit[4][8];
char face[13][8];

By the way, be careful. You want suit to have 8 characters per name,
but "diamonds" has exactly 8, which will discard the trailing \0
character, rendering that string unusable with standard C string
functions (such as strcpy()).

But actually, it would be easier with std::string:

# include <string>

int main()
{
std::string suit[4] = {"diamonds", "hearts", "clubs", "spades"};
std::string face[13] = {"Ace", "One" ... "King"};
}
};

deckOfCards::deckOfCards()
{
suit[4][8] = {"Diamonds", "Hearts", "Clubs", "Spades"};
That's illegal. You cannot have that kind of initialization in classes
and the syntax before the equal sign is ill-formed, it makes no sense.
You'll have to copy the names using strcpy(). Better still, use
std::string.
char *face[13][8] = {"Ace", "One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Jack", "Queen", "King"};
This is illegal. Drop the *. You want 13 elements of 8 characters each.
What's more, you are defining a local variable named face, not using
the member variable face. So that gives

face[13][8] = {..};

which is also illegal, as was the first statement.

strcpy(suit[0], "diamond");
strcpy(suit[1], "hearts");
// ...
}


Here's an example with std::string:

# include <string>

class Deck
{
public:
Deck()
{
suit[0] = "Diamonds";
suit[1] = "Hearts";
// ...

face[0] = "Ace";
face[1] = "One";
// ..
}

private:
std::string suit[4];
std::string face[13];
};
Jonathan

Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.