Initializing 2D array in constructor

A

alexkcha

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"};
}
 
J

Jonathan Mcdougall

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top