"Must have class/struct/union"

This program is attempting to populate a vector of cards, shuffle them, and then deal them. I'm getting the following error message:

.\cardTester.cpp(37) : error C2065: 'hoyle' : undeclared identifier
.\cardTester.cpp(37) : error C2228: left of '.moreCards' must have class/struct/union
type is ''unknown-type''


I have 5 files below to handle the program:

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
//////////////////////////////////////////////////////////////////////
//                                                                     
// Filename: Card.h
// 
// 
//
//             
//                
// 
//
// Description:
//    Card class declaration
//    Member functions defined in Card.cpp
/////////////////////////////////////////////////////////////////////////

//prevent multiple inclusions of header file
#ifndef CARD_H
#define CARD_H

#include<string>

using namespace std;

class Card
{
public:
	Card( );
	Card( int, int );//constructor
	string toString();	

	static int suit;
	static int face;
	static const string faces[ 14 ];
	static const string suits[ 4 ];

};

#endif CARD_H 


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
///////////////////////////////////////////////////////////////////////
//                                                                     
// Filename: Card.cpp
//
// 
//
//              
//                 
// 
//
// Description:
//     Contains documentation of the Card class.
//    
/////////////////////////////////////////////////////////////////////////

#include "Card.h"

//default constructor
Card::Card()
{
}

//constructor that initializes two private data members, face and suit
Card::Card(int f, int s)
{
	const string faces[ 14 ] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
	const string suits[ 4 ] = { "Spades", "Clubs", "Hearts", "Diamonds" };
	face =  f;
	suit =  s;
}

///////////////////////////////////////////////////////////////////////
//
// Function: toString                                          
//                                                                   
// Description:
//    returns the card as a string                  
//                                                                
// Parameters:  
//    firstParam  : first parameter's description          
//    secondParam : second parameter's description               
//                                                       
// Returns:  
//    returnVal : description of what is returned to caller
//                                                                     
///////////////////////////////////////////////////////////////////////

string toString()
{
	return Card::faces[ Card::face ] + " of " + Card::suits[ Card::suit ];
}




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
///////////////////////////////////////////////////////////////////////
//                                                                     
// Filename: DeckOfCards.h
// 
// 
//
//            
//                 
// 
//
// Description:
//    DeckOfCards class declaration
//    Member functions defined in DeckOfCards.cpp
/////////////////////////////////////////////////////////////////////////

//prevent multiple inclusions of header file
#ifndef DECKOFCARDS_H
#define DECKOFCARDS_H

#include<vector>
#include "Card.h"
using namespace std;

class DeckOfCards
{
public:
	DeckOfCards( Card );
	void shuffle( );
	Card dealCard( );
	bool moreCards( );
	static vector< Card > deck;
	static int currentCard; //integer value to represent which card will be dealt
	
	
};


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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
///////////////////////////////////////////////////////////////////////
//                                                                     
// Filename: DeckOfCards.cpp
// 
// 
//
//          
//                  
// 
//
// Description:
//     Contains documentation of the DeckOfCards class.
//    
/////////////////////////////////////////////////////////////////////////
#include "DeckOfCards.h"
#include "Card.h"
#include<iostream>
#include <cstdlib>
#include <ctime>
#include <string>

//using namespace std;

//constructor that takes a Card parameter and adds to the deck vector
DeckOfCards::DeckOfCards( Card c )
{
	deck.push_back( c );
}

///////////////////////////////////////////////////////////////////////
//
// Function: shuffle                                          
//                                                                   
// Description:
//    iterates through the vector of cards and swaps two at a time                  
//                                                                
// Parameters:  
//                                                 
// Returns:  
//    
//                                                                     
///////////////////////////////////////////////////////////////////////

void shuffle()
{
	srand((unsigned)time(0));
	int random;
	int lowest = 0;
	int highest = 51;
	int range= ( highest - lowest ) +1; 
	for (int i = 0; i < 52; i++)
	{
		random = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
		Card temp;
		temp = DeckOfCards::deck[ i ];
		DeckOfCards::deck[ i ] = DeckOfCards::deck[ random ];
		DeckOfCards::deck[ random ] = temp;
	}
}

///////////////////////////////////////////////////////////////////////
//
// Function: dealCard                                          
//                                                                   
// Description:
//    returns the next card from the deck vector                 
//                                                                
// Parameters:  
//                                                 
// Returns: 
//    currentCard the card that is to be dealt
//                                                                     
///////////////////////////////////////////////////////////////////////

Card dealCard()
{
	cout << DeckOfCards::deck[ DeckOfCards::currentCard ].toString() << endl;
	return DeckOfCards::deck[ DeckOfCards::currentCard ];
}

///////////////////////////////////////////////////////////////////////
//
// Function: moreCards                                         
//                                                                   
// Description:
//    checks to see whether there is another card in the deck                 
//                                                                
// Parameters:  
//                                                 
// Returns: 
//    boolean value testing if there is another card in the deck
//                                                                     
///////////////////////////////////////////////////////////////////////

bool moreCards()
{
	if ( DeckOfCards::currentCard + 1 < 53 )
		return true;
	else return false;
}


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
///////////////////////////////////////////////////////////////////////
//                                                                     
// Filename: cardTester.cpp
// 
// 
//
//             
//                 
// 
//
// Description:
//     Program to test classes DeckOfCards and Cards.
//     
/////////////////////////////////////////////////////////////////////////
#include<iostream>
#include "DeckOfCards.h"
#include "DeckOfCards.cpp"
#include "Card.h"
#include "Card.cpp"

using namespace std;

int main()
{
	for (int i = 0; i < 52; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			for (int k = 0; k < 13; k++)
			{
				Card c(k, j);
				DeckOfCards hoyle( c );
				hoyle.shuffle();
			}
		}
	}
	if ( hoyle.moreCards().isTrue() )
	{
		hoyle.dealCard();
		DeckOfCards::currentCard++;
	}

	system( "pause" );
	return 0;
}


Is the compiler not recognizing hoyle as a DeckOfCards object?
An object only exists in the block it's defined in.
For example:
1
2
3
4
5
6
7
8
//...
{ // This is where the block starts.
    std::cout <<a; // Error. a not declared.
    int a=10;
    std::cout <<a; // OK.
} // This is where the block ends. Any objects declared in the block go out of scope here.
std::cout <<a; // Error. a not declared.
//... 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Card
{
public:
	static int suit; //static? all the cards have the same suit and the same face?
	static int face;
};


Card::Card(int f, int s)
{
//those don't go here. You're just creating 2 string that die right away.
	const string faces[ 14 ] = { /*     */};
	const string suits[ 4 ] = {  /*     */ };

}
//Instead put in Card.cpp
const string Card::faces[14] = {/*   */};
const string Card::suits[4] = {/*   */};
Last edited on
Topic archived. No new replies allowed.