Calling a static array from a dynamic memory

So I was making a programm with a class bingocard and a class player. Each player can have a maxium number od bingocards which you give it in the constructor of the player class. In the constructor we also have a dynamic array where we save each bingocard which is a 4x4 matrix (2d array).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    class Bingocard
    {
    private:
    	unsigned int card[4][4];
    
    public:
    	Bingocard();
    	Bingocard(unsigned int Card[4][4]);
    	Bingocard(const Bingocard& Card);
    	Bingocard operator=(const Bingocard& Card);
    };
    
    BingocardBingocard()
    {
    }
    
    Bingocard::Bingocard(unsigned int Card[4][4])
    {
    for (int i=0; i<4; i++)
    	for (int j=0; j<4; j++)
    		card[i][j] = Card[i][j];
    }
    
    Bingocard::Bingocard(const Bingocard& Card)
    {
    	for (int i = 0; i < 4; i++)
    		for (int j = 0; j < 4; j++)
    			card[i][j] = Card.card[i][j];
    }
    
    Bingocard Bingocard::operator=(const Bingocard& Card)
    {
    	for (int i = 0; i < 4; i++)
    		for (int j = 0; j < 4; j++)
    			this->card[i][j] = Card.card[i][j];
    
    	return (*this);
    }
    
    class Player
    {
    private:
    	unsigned int maxNumberCards;
    	Bingocard* cards;
    	unsigned int numberCards;
    
    public:
    	Player(unsigned int k);
    	void newCard(Bingocard* Card);
    	~Player();
    };
    
    Player::Player(unsigned int k)
    {
    	numberCards = 0;
    	maxNumberCards = k;
    	cards = new Bingocard[maxNumberCards];
    }
    
    void Player::newCard(Bingocard* card)
    {
    	if (numberCards < maxNumberCards) {
    		cards[anzahlKarten] = *card; //Assign operator is triggered;
    		anzahlKarten += 1;
    	}
    	else
    		cout << "Maximum cards reached! Adding more cards is not possible now." << endl;
    }
    
    Player::~Player()
    {
    delete[] cards;
    }

I have now that dynamic array called cards in the class Player and I want to access each of its elements but I dont know which command to use. I know I can make a function for example getElement and give it the row and column I want as parameters, but I was wondering if I can do that the same thing without a function. I tried (cards[a])[b][c] but it gives an error back.
I need this to work so I can check each number on the card for a bingo.
Last edited on
No. You've made a whole new class named Bingocard, with the data it contains being private. You have to make a public function if you want to get that data back out.
Maybe making the Player class a friend class in Bingocard and then overloading the [] operator?
But I dont know how to return a 2d static array back... Whatever I try, it gives me an error back.
I need this to work so I can check each number on the card for a bingo.


Instead of accessing the array from outside the class, maybe you could make the checking of the numbers a member function of the Bingocard class.
Last edited on
I need this to work so I can check each number on the card for a bingo.

What's not working?

Line 13: Needs ::
1
2
Bingocard::Bingocard()
{}


line 63,64: anzahlKarten is undefined. Do you mean numberCards ?

Line 57: You're allocating maxNumberCards. Why do you even have Player::newCard() ?
This is going to cause a memory leak because you've already allocated the array of cards in your constructor.

Lines 55,56: These seem redundant. If you're allocating the maximum number of cards in your constructor, then numberCards a maxNumberCards serve the same purpose.

BTW, a traditional bingo card has 5 rows and columns.

As a matter of good style, I would suggest adding after line 2:
 
 static const unsigned CARD_SIZE = 4;  // or 5 if you choose 

Then every place you hard code a 4, use CARD_SIZE instead. This will allow you to easily change the size of the bingo card without making lots of changes.

Since you've made card private to BingoCard, then all access to the card array must be through functions provided in the BingoCard class. Player should not need to know how card is represented inside BingoCard.

Edit:
You can simplify two of your constructors and your assignment operator by providing a Copy function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Copy (const int Card[CARD_SIZE][CARD_SIZE])
        {   for (int i=0; i<CARD_SIZE; i++)
    	        for (int j=0; j<CARD_SIZE; j++)
    		        card[i][j] = Card[i][j];
    	}
//...
   Bingocard::Bingocard(int Card[CARD_SIZE][CARD_SIZE])
    {   Copy (Card);    
    }
    
    Bingocard::Bingocard(const Bingocard& Card)
    {   Copy (Card.card);
    }
    
    Bingocard & Bingocard::operator=(const Bingocard& Card)
    {   Copy (Card.card);
    	return (*this);
    }


Line 31: BingoCard should be returned by reference, not value.

You need to keep two pieces of information for each cell on a bingo card before you can check for a winner:
- The value (number) of the cell.
- Whether the number has been drawn yet.
Currently, you don't appear to be initializing your BingoCard anywhere.
I would suggest adding a bool array to your BingoCard class.
1
2
 
  bool drawn[CARD_SIZE][CARD_SIZE]

Don't forget to initialize the bool array to false.

You're also going to need a bool array for the numbers drawn by the caller (computer).
Last edited on
Topic archived. No new replies allowed.