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

时间:2012-09-13 15:14:15

标签: c++ include compiler-errors g++ ifndef

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

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

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

football.cpp

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

team.h

#ifndef __game_h_
#define __game_h_
#endif

team.cpp

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

game.h

#ifndef __date_h_
#define __date_h_
#endif

game.cpp

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

date.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 个答案:

答案 0 :(得分:5)

我认为您误解了使用包含警卫

#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

答案 1 :(得分:1)

看起来你有两个问题:第一个是你在源文件中使用包含警戒;第二个是你包含文件的顺序是错误的。

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

<强> “game.h”

class Game {};

<强> “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 定义的,因此会跳过声明。

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

破解“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

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

<强> “game.h”

#ifndef GAME
#define GAME

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

#endif

<强> “date.h”

#ifndef GAME
#define GAME

class Date
{
};

#endif

<强> “game.cpp”

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

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

<强> “game.cpp”

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

或在“game.h”中加入“date.h”:

<强> “game.h”

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

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

#endif

<强> “game.cpp”

#include "game.h"  // Still fine, "date.h" is included by "game.h"

答案 2 :(得分:0)

你不需要c ++文件中的警卫

答案 3 :(得分:0)

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

如果你有

BaseClass.h
struct HumanEntity { }

然后有不同的类使用该基类,没有包含保护可能会遇到问题。这就是你放

的原因
#ifndef __MYGUARD
#define __MYGUARD

... // here is your header code

#endif