alexkcha@gmail.com wrote:[color=blue]
> 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?[/color]
Several things are wrong. What book are you reading? See
www.accu.org
for suggestions and read
http://www.parashift.com/c++-faq-lite/.
[color=blue]
> #include <stdio.h>
> #include <string>[/color]
What don't you use std::string??
[color=blue]
>
> using namespace std;
>
> class deckOfCards{
> public:
> deckOfCards();
>
> private:
> char* suit[4][8];
> char* face[13][8];[/color]
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"};
}
[color=blue]
> };
>
> deckOfCards::deckOfCards()
> {
> suit[4][8] = {"Diamonds", "Hearts", "Clubs", "Spades"};[/color]
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.
[color=blue]
> char *face[13][8] = {"Ace", "One", "Two", "Three", "Four", "Five",
> "Six", "Seven", "Eight", "Nine", "Jack", "Queen", "King"};[/color]
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");
// ...
[color=blue]
> }[/color]
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