I am a newbie here, so I hope I do everything right. Problem: I watched the Lynda.com C++ Tutorial with Bill Weinman and there is a example code, which prints out all the cards from a usual poker deck. The struct defines a single card and the array contains every 52 cards. The code works, if everything is written in the main class. My problem is to seperate this code and put it in an extra class. I don't know why.
#include <cstdio>
#include <iostream>
//#include "Cards.h"
using namespace std;
struct card{
unsigned int rank;
unsigned int suit;
};
enum card_suit { HERZ, KARO, PIK, KREUZ };
enum card_rank { ASS = 1, BUBE = 11, DAME = 12, KOENIG = 13 };
const char * assString = "Ass";
const char * bubeString = "Bube";
const char * dameString = "Dame";
const char * koenigString = "Koenig";
const char * herzString = "Herz";
const char * karoString = "Karo";
const char * pikString = "Pik";
const char * kreuzString = "Kreuz";
card deck[52] = {
{ ASS, HERZ }, { 2, HERZ }, { 3, HERZ }, { 4, HERZ }, { 5, HERZ }, { 6, HERZ },
{ 7, HERZ }, { 8, HERZ }, { 9, HERZ }, { 10, HERZ }, { BUBE, HERZ }, { DAME, HERZ }, { KOENIG, HERZ },
{ 1, KARO }, { 2, KARO }, { 3, KARO }, { 4, KARO }, { 5, KARO }, { 6, KARO },
{ 7, KARO }, { 8, KARO }, { 9, KARO }, { 10, KARO }, { 11, KARO }, { 12, KARO }, { 13, KARO },
{ ASS, PIK }, { 2, PIK }, { 3, PIK }, { 4, PIK }, { 5, PIK }, { 6, PIK },
{ 7, PIK }, { 8, PIK }, { 9, PIK }, { 10, PIK }, { BUBE, PIK }, { DAME, PIK }, { 13, PIK },
{ ASS, KREUZ }, { 2, KREUZ }, { 3, KREUZ }, { 4, KREUZ }, { 5, KREUZ }, { 6, KREUZ },
{ 7, KREUZ }, { 8, KREUZ }, { 9, KREUZ }, { 10, KREUZ }, { BUBE, KREUZ }, { 12, KREUZ }, { 13, KREUZ }
};
void printCards(const card & c){
switch (c.suit){
case HERZ:
printf("%s ", herzString);
break;
case KARO:
printf("%s ", karoString);
break;
case PIK:
printf("%s ", pikString);
break;
case KREUZ:
printf("%s ", kreuzString);
break;
}
if (c.rank >= 2 && c.rank <= 10){
printf("%d\n", c.rank);
}
else {
switch (c.rank){
case ASS:
printf("%s\n", assString);
break;
case BUBE:
printf("%s\n", bubeString);
break;
case DAME:
printf("%s\n", dameString);
break;
case KOENIG:
printf("%s\n", koenigString);
break;
}
}
}
int main(int argc, char ** argv) {
for (auto & c : deck){
printCards(c);
}
system("PAUSE");
return 0;
}
So here is what I did:
main class:
#include <cstdio>
#include <iostream>
#include "Cards.h"
using namespace std;
int main(int argc, char ** argv) {
Cards poker;
for (auto & c : poker.deck){
poker.printCards(c);
}
system("PAUSE");
return 0;
}
Cards.h
#pragma once
#include <cstdio>
#include <string>
using namespace std;
struct card{
unsigned int rank;
unsigned int suit;
};
class Cards
{
public:
Cards();
~Cards();
enum card_suit { HERZ, KARO, PIK, KREUZ };
enum card_rank { ASS = 1, BUBE = 11, DAME = 12, KOENIG = 13 };
const char * assString = "Ass";
const char * bubeString = "Bube";
const char * dameString = "Dame";
const char * koenigString = "Koenig";
const char * herzString = "Herz";
const char * karoString = "Karo";
const char * pikString = "Pik";
const char * kreuzString = "Kreuz";
card deck[52] = {
{ ASS, HERZ }, { 2, HERZ }, { 3, HERZ }, { 4, HERZ }, { 5, HERZ }, { 6, HERZ },
{ 7, HERZ }, { 8, HERZ }, { 9, HERZ }, { 10, HERZ }, { BUBE, HERZ }, { DAME, HERZ }, { KOENIG, HERZ },
{ 1, KARO }, { 2, KARO }, { 3, KARO }, { 4, KARO }, { 5, KARO }, { 6, KARO },
{ 7, KARO }, { 8, KARO }, { 9, KARO }, { 10, KARO }, { 11, KARO }, { 12, KARO }, { 13, KARO },
{ ASS, PIK }, { 2, PIK }, { 3, PIK }, { 4, PIK }, { 5, PIK }, { 6, PIK },
{ 7, PIK }, { 8, PIK }, { 9, PIK }, { 10, PIK }, { BUBE, PIK }, { DAME, PIK }, { 13, PIK },
{ ASS, KREUZ }, { 2, KREUZ }, { 3, KREUZ }, { 4, KREUZ }, { 5, KREUZ }, { 6, KREUZ },
{ 7, KREUZ }, { 8, KREUZ }, { 9, KREUZ }, { 10, KREUZ }, { BUBE, KREUZ }, { 12, KREUZ }, { 13, KREUZ }
};
void printCards(const card & c);
};
Cards.cpp
#include "Cards.h"
Cards::Cards()
{
}
Cards::~Cards()
{
}
void Cards::printCards(const card & c){
switch (c.suit){
case HERZ:
printf("%s ", herzString);
break;
case KARO:
printf("%s ", karoString);
break;
case PIK:
printf("%s ", pikString);
break;
case KREUZ:
printf("%s ", kreuzString);
break;
}
if (c.rank >= 2 && c.rank <= 10){
printf("%d\n", c.rank);
}
else {
switch (c.rank){
case ASS:
printf("%s\n", assString);
break;
case BUBE:
printf("%s\n", bubeString);
break;
case DAME:
printf("%s\n", dameString);
break;
case KOENIG:
printf("%s\n", koenigString);
break;
}
}
}
I know, it's a really long code, but I don't know where I should reduce it. In the end there is this error:
Error 1 error C2536: 'Cards::Cards::deck' : cannot specify explicit initializer for arrays
I hoe you can help me out. Even if I put static in front of the "deck" I am getting an error. I showed this code two people and they couldn't help me and I searched on this site, but often an array of structs are in the main. So this works, but I have a problem with seperating it.
Thanks