0

好的,所以我是一名 HTML/Javascript/PHP 专业人士,但我正在尝试学习 C++。我还是个新手,我有一个 C++ 编程项目,但我有错误。

包含 int main() 的文件是“football.cpp”,我有三个必须使用的类的 .h 和 .cpp 文件:Game、Team 和 Date。每个 Team 实例都包含一个 Games 向量,每个 Game 包含一个 Date。Date 不包含任何其他类的实例。

我正在尝试找到一种在文件顶部使用 #include 和 #ifndef 语句的方法,以便在编译时不会出错,但我还没有找到有效的组合。我不确定是否还有其他错误。这是我当前的#include 部分,不包括其他库:

足球.cpp

#include "game.h"
#include "team.h"
#include "date.h"

团队.h

#ifndef __game_h_
#define __game_h_
#endif

团队.cpp

#include "team.h"
#ifndef __game_h_
#define __game_h_
#endif

游戏.h

#ifndef __date_h_
#define __date_h_
#endif

游戏.cpp

#include "game.h"
#ifndef __date_h_
#define __date_h_
#endif

日期.cpp

#include "date.h"

我使用 Cygwin g++ 编译器,我用来编译它的行是:

g++ football.cpp team.cpp game.cpp date.cpp -o football.exe

这是我得到的所有错误:(警告,文字墙)

$ g++ football.cpp team.cpp game.cpp date.cpp -o football.exe

In file included from football.cpp:9:0:
game.h:15:96: error: ‘Date’ has not been declared
game.h:24:1: error: ‘Date’ does not name a type
game.h:37:1: error: ‘Date’ does not name a type
football.cpp: In function ‘int main(int, char*)’:
football.cpp:65:70: error: no matching function for call to ‘Game::Game(std::string&, std::string&, int [5], int [5], Date&)’
game.h:15:1: note: candidates are: Game::Game(std::string, std::string, const int, const int*, int)
game.h:14:1: note: Game::Game()
game.h:10:12: note: Game::Game(const Game&)
In file included from team.cpp:4:0:
team.h:24:8: error: ‘Game’ was not declared in this scope
team.h:24:12: error: template argument 1 is invalid
team.h:24:12: error: template argument 2 is invalid
team.cpp: In member function ‘float Team::getStat3()’:
team.cpp:36:26: error: request for member ‘size’ in ‘((Team*)this)->Team::games’, which is of non-class type ‘int’
team.cpp:37:21: error: invalid types ‘int[int]’ for array subscript
team.cpp:37:50: error: invalid types ‘int[int]’ for array subscript
team.cpp:38:21: error: invalid types ‘int[int]’ for array subscript
team.cpp:38:47: error: invalid types ‘int[int]’ for array subscript
team.cpp:38:76: error: invalid types ‘int[int]’ for array subscript
team.cpp:38:106: error: invalid types ‘int[int]’ for array subscript
team.cpp: In function ‘bool compare2(Team, Team)’:
team.cpp:45:39: error: request for member ‘size’ in ‘t1.Team::games’, which is of non-class type ‘const int’
team.cpp:46:39: error: request for member ‘size’ in ‘t2.Team::games’, which is of non-class type ‘const int’
team.cpp:50:17: error: request for member ‘size’ in ‘t1.Team::games’, which is of non-class type ‘const int’
team.cpp:50:35: error: request for member ‘size’ in ‘t2.Team::games’, which is of non-class type ‘const int’
team.cpp:52:24: error: request for member ‘size’ in ‘t1.Team::games’, which is of non-class type ‘const int’
team.cpp:52:43: error: request for member ‘size’ in ‘t2.Team::games’, which is of non-class type ‘const int’
team.cpp: In function ‘bool compare3(Team, Team)’:
team.cpp:62:29: error: passing ‘const Team’ as ‘this’ argument of ‘float Team::getStat3()’ discards qualifiers
team.cpp:63:29: error: passing ‘const Team’ as ‘this’ argument of ‘float Team::getStat3()’ discards qualifiers
In file included from game.cpp:5:0:
game.h:15:96: error: ‘Date’ has not been declared
game.h:24:1: error: ‘Date’ does not name a type
game.h:37:1: error: ‘Date’ does not name a type
game.cpp: In constructor ‘Game::Game()’:
game.cpp:26:3: error: ‘date’ was not declared in this scope
game.cpp:26:15: error: ‘Date’ was not declared in this scope
game.cpp: At global scope:
game.cpp:29:94: error: ‘Date’ has not been declared
game.cpp:29:1: error: prototype for ‘Game::Game(std::string, std::string, int*, int*, int)’ does not match any in class ‘Game’
game.h:10:12: error: candidates are: Game::Game(const Game&)
game.h:15:1: error: Game::Game(std::string, std::string, const int*, const int*, int)
game.cpp:13:1: error: Game::Game()
game.cpp: In member function ‘int Game::getVisitingScore(int) const’:
game.cpp:80:10: error: ‘visitingScores’ was not declared in this scope
game.cpp: At global scope:
game.cpp:94:1: error: ‘Date’ does not name a type


如果需要进一步澄清,将进行编辑,尽管我认为不会。

任何帮助将不胜感激。

4

4 回答 4

6

我认为您误解了include guards的使用。

#ifndef __game_h_
#define __game_h_
// ...
#endif

上述语句集通常仅用于描述游戏界面的头文件中,以防止多次包含该头文件。

但是在您的代码中,您已在标头实现中添加了包含保护,而且您似乎混淆了实体 - 除非我误解了您的文件内容 - 例如在team.h中,您似乎具有包含保护游戏

我的猜测是,您对包含守卫的滥用实际上是在阻止定义某些类型。

正如其他人所提到的,不要使用以双下划线开头的名称。

例如,将包含保护添加到您的团队标题中以防止该文件被多次包含,并为包含保护指定一个反映他们正在保护的模块的名称:

// team.h
#ifndef TEAM_H
#define TEAM_H

// ... code for team.h in here

#endif    // TEAM_H
于 2012-09-13T15:17:31.143 回答
1

看起来您在这里有两个问题:第一个是您在源文件中使用了包含防护;第二个是您包含文件的顺序错误。

首先,包括警卫。其他人已经回答了这个问题,所以我会简短。包含保护仅在头文件中使用,以防止多次声明类/类型。例如,如果我有:

“游戏.h”

class Game {};

“游戏.cpp”

#include "game.h"
#include "game.h"  // ERROR:  class Game is declared twice.

为了解决这个问题,我会在“game.h”中使用包含警卫:

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

class Game {};

#endif

第一次包含该文件时,未定义 GAME_H_INCLUDED,因此声明了 Game。第二次是include,定义了GAME_H_INCLUDED 所以跳过了声明。

您遇到的问题是源文件中的包含防护将导致跳过所有实现:

损坏的“game.cpp”

#include "game.h"
#ifndef GAME_H_INCLUDED  //  This is defined in "game.h" so everything will be
                         //  skipped until #endif is encountered.
#define GAME_H_INCLUDED

//  Implementation of Game class

#endif

其次,您包含标题的顺序不正确。我猜你有这样的事情:

“游戏.h”

#ifndef GAME
#define GAME

class Game
{
    Date mDate;  // Member of type Date, declared in "date.h"
};

#endif

“日期.h”

#ifndef GAME
#define GAME

class Date
{
};

#endif

“游戏.cpp”

#include "game.h"  // ERROR:  Game relies on the Date class,
                   //         which isn't included yet
#include "date.h"

要解决此问题,请交换“game.cpp”中包含的顺序:

“游戏.cpp”

#include "date.h"
#include "game.h"  // Fine, we know what a Date is now.

或者在“game.h”中包含“date.h”:

“游戏.h”

#include "date.h"
#ifndef GAME
#define GAME

class Game
{
    Date mDate;  // Member of type Date, declared in "date.h"
};

#endif

“游戏.cpp”

#include "game.h"  // Still fine, "date.h" is included by "game.h"
于 2012-09-13T16:42:50.623 回答
0

您不需要 c++ 文件中的守卫

于 2012-09-13T15:17:07.613 回答
0

错误是您应该仅在头文件中使用包含防护

如果你有一个

BaseClass.h
struct HumanEntity { }

然后让不同的类使用该基类,如果没有包含保护,您可能会遇到问题。这就是为什么你放一个

#ifndef __MYGUARD
#define __MYGUARD

... // here is your header code

#endif
于 2012-09-13T15:21:48.287 回答