Question

I am instructed to code a Blackjack project for my college CMSC class. I have made all required source files but there is an error that I cannot figure out. I am using the terminal with a Makefile to compile my program.

When I compile my program I get this error in the terminal along with other warnings (I am not concerned about the warnings).

In file included from Blackjack.h:19:0,
                 from Proj2.cpp:12:

Player.h:17:3: error: ‘Hand’ does not name a type
   Hand hand;
   ^

In file included from Blackjack.h:19:0,
                 from Blackjack.cpp:1:

Player.h:17:3: error: ‘Hand’ does not name a type
   Hand hand;

Here is my source code in a Github repository.

https://github.com/Terrablezach/Blackjack

Can anyone tell me why the class 'Hand' does not name a type? I have included it in my header files where it needs to be included and I don't understand why it does not recognize it as a class.

Thank you in advance for your help.

Was it helpful?

Solution 2

Looking at the project brief you can change code, correct errors and include orders, however it states you cannot change the function declarations (this will be to limit the possible number of variations in code functionality I would suspect).

The function declaration is simply this line: Player(char *newName, int newFunds)

Looking at your code you are potentially going to run into problems with circular inclusion in your headers.

What you could do is wrap each header in a small piece of logic to prevent the same file from being included multiple times, for example add the lines

#pragma once
//  the #pragma effectively does the same as the #ifndef/#define lines, 
//  its the equivalent of belt and braces if you use both
#ifndef HAND_H
#define HAND_H
//normal hand.h code in here
#endif

that way no matter how many times you call on the hand.h file you cannot end up with a multiply defined/included header. As a matter of rote I do that with all my header files whilst doing rapid development.

In regards specifically to the error Player.h:17:3: error: ‘Hand’ does not name a type Hand hand; I suspect the previous comment in regards to the include order is correct, however I have no linux environment to hand, but will get back to you later tonight/tomorrow:)

OTHER TIPS

You haven't included Hand.h in Player.h, so the definition of Hand is not available there.

The order of the #include declarations is incorrect.

Player class is dependant upon the declaration of Hand class. So in Blackjack.h the #include for Hand.h must go before the #include for Player.h

#ifndef BLACKJACK_H
#define BLACKJACK_H

#include <vector>
#include "Hand.h"    // must be before Player.h include
#include "Player.h"

Alternatively, a forward declaration can be used in Player.h.

class Hand;  // forward declaration of class Hand

class Player {
 public:
  Player();
  Player(char *newName, int newFunds);
  ...
  ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top