C ++ - 类没有命名类型

时间:2014-03-18 10:33:00

标签: c++ linux ubuntu debian ubuntu-13.10

我被指示为我的大学CMSC课程编写一个Blackjack项目。我已经制作了所有必需的源文件,但是有一个我无法弄清楚的错误。我正在使用带有Makefile的终端来编译我的程序。

当我编译程序时,我在终端中收到此错误以及其他警告(我不关心警告)。

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;

这是我在Github存储库中的源代码。

https://github.com/Terrablezach/Blackjack

有谁可以告诉我为什么班级'手'没有命名类型?我已将它包含在我需要包含的头文件中,我不明白为什么它不能将其识别为类。

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您尚未在Hand.h中加入Player.h,因此Hand的定义不可用。

答案 1 :(得分:1)

#include声明的顺序不正确。

Player类依赖于Hand类的声明。所以在Blackjack.h中,Hand.h的#include必须在之前为Player.h的#include

#ifndef BLACKJACK_H
#define BLACKJACK_H

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

或者,可以在Player.h中使用前向声明。

class Hand;  // forward declaration of class Hand

class Player {
 public:
  Player();
  Player(char *newName, int newFunds);
  ...
  ...

答案 2 :(得分:1)

查看项目简介,您可以更改代码,更正错误并包含订单,但是它声明您无法更改函数声明(这将限制我怀疑的代码功能的可能变化数量。)

函数声明就是这一行:Player(char *newName, int newFunds)

查看您的代码,您可能会遇到标题中循环包含的问题。

你可以做的是将每个标题包装在一小段逻辑中,以防止多次包含同一个文件,例如添加行

#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

这样,无论你多少次调用hand.h文件,你都不能得到一个多重定义/包含的头。作为一个死记硬背的问题,我在快速开发的同时使用我的所有头文件。

特别针对错误Player.h:17:3: error: ‘Hand’ does not name a type Hand hand;我怀疑之前关于包含顺序的评论是正确的,但是我没有可用的linux环境,但会在今晚/明天晚些时候回复你:)< / p>