Trying to create an array of objects in a class

Hi. I'm getting compiler errors in the following code.

//deck.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef __DECK
#define __DECK

#include "card.h"
#include "player.h"
#include <string>

class deck {
private:
	card card_in_pos[24]; // C4430: missing type specifier - int assumed. 
// Note: C++ does not support default-int, 
// CC2146: syntax error : missing ';' before identifier 'card_in_pos'
public:

	deck();
	void shuffle();
	void deal();
};

#endif 


// card.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef _EUCHRE__CARD
#define _EUCHRE__CARD

#include "deck.h"
#include "player.h"

class card {
public:
	char value;
	char suit;
public:
	card();
	card( char, char );
	friend class deck;
	friend class player;
};

#endif 
circular inclusion problem.

Don't #include "deck.h" in card.h -- you don't need it.

Ironically, I'm just about done writing an article on this very subject.
This error occurs because each file includes the other, a potentially tricky situation. You need forward declarations. Try putting class card; just before class deck { in deck.h and class deck; just before class card { in card.h.

EDIT: As Disch has pointed out, you don't actually need to include deck.h in your case since you are only referencing deck as a friend. See his excellent article on the subject in the articles section.
Last edited on
Topic archived. No new replies allowed.