C++尝试使用 #ifndef 和 #include 语句

C++ trying to use #ifndef and #include statements

本文关键字:#include 语句 #ifndef C++      更新时间:2023-10-16

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

包含int main((的文件是'football.cpp',我有我必须使用的三个类的.h和.cpp文件:游戏,球队和日期。 团队的每个实例都包含一个游戏向量,每个游戏都包含一个日期。 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

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

任何帮助将不胜感激。

我认为

你误解了包含防护的使用。

#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

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

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

"游戏.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。 第二次包含时,GAME_H_INCLUDED定义,因此跳过声明。

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

破碎的"游戏.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"

你不需要 C++ 文件中的守卫

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

如果您有

BaseClass.h
struct HumanEntity { }

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

#ifndef __MYGUARD
#define __MYGUARD
... // here is your header code
#endif