c++linuxubuntudebianubuntu-13.10

C++ - Class does not name a type


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.


Solution

  • 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:)