1

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

  • see http://stackoverflow.com/questions/23900191/error-cannot-specify-explicit-initializer-for-array – Grv Oct 27 '15 at 09:58
  • Thanks for this quick answer. Now I know the problem, but it isn't solved yet. If I declare the array in the constructor in the Cards.cpp file and declare in the Cards.h file only `card new_deck[52]`, the output is wrong. There is only printed "Herz, Herz, Herz...." and so on.... –  Oct 27 '15 at 10:29

2 Answers2

0

Like the error message says, C style arrays are not first class citizens in C++ and cannot be initialized like that in a class.

I would declare a new_deck outside the class and copy from that in the class' constructor.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Thanks! So I declared in the Cards.h, but inside the class Cards (because outside there is an error) `card new_deck[52];` and in the Cards.cpp I declared the array with all the cards. So in the main, I have written this `for (auto & c : poker.new_deck){poker.printCards(c);}` and it gives me the wrong output. I can't explain why... Thank you for yor help! –  Oct 27 '15 at 10:35
0

You can't assign values to a variables inside a class definition (as you are doing with card array, strings). You need to create a .cpp file for your class, and initialize those variables in the constructor of that class.

Even if you are using static variables, you must declare them outside of class, and assign value to them, there.

Algirdas Preidžius
  • 1,769
  • 3
  • 14
  • 17
  • Thanks for the quick answer. I understand the problem. It's a bit sad, that the tutorial contains so much C stuff... So if I am declare it in the constructor, there is no error, but the output is wrong. "Herz, Herz, Herz..." and so on. –  Oct 27 '15 at 10:31
  • @andishanti So, did you: a) moved whole array declaration to the constructor, or b) moved only instantiation of the array to the constructor? – Algirdas Preidžius Oct 27 '15 at 10:42
  • Sorry for the confusion. I moved the whole initialisation of the array into the constructor, but left the declaration `card new_deck[52];` in the header file. –  Oct 27 '15 at 10:48