简体   繁体   English

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

[英]C++ trying to use #ifndef and #include statements

Ok, so I'm a HTML/Javascript/PHP professional, but I'm trying to learn C++. 好的,所以我是HTML / Javascript / PHP专业人员,但是我正在尝试学习C ++。 I'm still a newbie at it, and I have a C++ programming project that I have errors with. 我仍然是一个新手,并且我有一个C ++编程项目,但我遇到了错误。

The file that contains int main() is 'football.cpp', and I have .h and .cpp files for three classes that I have to use: Game, Team, and Date. 包含int main()的文件是“ football.cpp”,对于三个必须使用的类,我拥有.h和.cpp文件:游戏,团队和日期。 Each instance of Team contains a vector of Games, and each Game contains a Date. Team的每个实例包含一个游戏向量,每个Game包含一个日期。 Date does not contain instances of any other class. 日期不包含任何其他类的实例。

I am trying to find a way to use #include and #ifndef statements at the tops of the files so that I don't get errors when I compile, but I haven't found a combination that works. 我试图找到一种在文件顶部使用#include和#ifndef语句的方法,以便在编译时不会出现错误,但是我没有找到一种有效的组合。 I am not sure if there are other bugs. 我不确定是否还有其他错误。 Here's my current #include sections, not counting other libraries: 这是我当前的#include部分,不包括其他库:

football.cpp 足球

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

team.h 团队

#ifndef __game_h_
#define __game_h_
#endif

team.cpp team.cpp

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

game.h 游戏

#ifndef __date_h_
#define __date_h_
#endif

game.cpp game.cpp

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

date.cpp date.cpp

#include "date.h"

I use Cygwin g++ compiler, and the line I use to compile it is: 我使用Cygwin g ++编译器,用于编译的行是:

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

Here are all the errors I get: (WARNING, WALL OF TEXT) 这是我得到的所有错误:(警告,WALL OF TEXT)

$ 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


Will edit if further clarification is needed, although I don't think it will be. 如果不需要进一步说明,将进行编辑,尽管我认为不是。

Any help would be vastly appreciated. 任何帮助将不胜感激。

I think that you're misunderstanding the use of include guards . 我认为您误解了包含卫队的使用。

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

The set of statements above would typically be used only in the header file describing your game interface, to prevent multiple-inclusion of that header file. 上面的语句集通常仅用于描述游戏界面的头文件中,以防止该头文件被多个包含。

But in your code, you've added include guards to header and implementation, and also you seem to be confusing entities - unless I'm misunderstanding your file contents - for instance in team.h , you have what appear to be include guards for game . 但是在您的代码中,您已经为标头实现添加了包含保护,并且您似乎还使实体感到困惑-除非我误解了文件内容,例如在team.h中 ,您似乎拥有了针对这些对象的包含保护游戏

My guess is that your misuse of include guards is actually preventing some types from being defined at all. 我的猜测是,您对包含保护的滥用实际上实际上阻止了某些类型的定义。

As others have mentioned, don't use names beginning with double underscores. 正如其他人提到的那样,请勿使用以双下划线开头的名称。

As an example, add include guards to your team header to prevent multiple-inclusion of that file, and give the include guards a name that reflects the module that they're guarding: 例如,在您的团队标题中添加包含保护,以防止对该文件进行多次包含,并为包含保护提供一个名称,以反映其所保护的模块:

// team.h
#ifndef TEAM_H
#define TEAM_H

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

#endif    // TEAM_H

It looks like you have 2 problems here: The first is that you are using include guards in your source files; 看来您这里有2个问题:第一个问题是您在源文件中使用了包含保护。 the second is that the order in which you have included your files is wrong. 第二个原因是您包含文件的顺序错误。

First, include guards. 首先,包括警卫。 Others have answered this question already, so I'll be brief. 其他人已经回答了这个问题,所以我将简短介绍。 Include guards are used only in header files in order to prevent classes/types being declared more than once. 包含保护仅用于头文件中,以防止多次声明类/类型。 For example if I had: 例如,如果我有:

"game.h" : “ game.h”

class Game {};

"game.cpp" : “ game.cpp”

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

To fix this I would use include guards in "game.h": 为了解决这个问题,我将在“ game.h”中使用include guards:

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

class Game {};

#endif

The first time the file is included, GAME_H_INCLUDED is not defined, so Game is declared. 第一次包含文件时,未定义GAME_H_INCLUDED,因此声明了Game。 The second time it is include, GAME_H_INCLUDED is defined, so the declaration is skipped. 在第二时间被包括,GAME_H_INCLUDED 定义,所以该声明被跳过。

The problem you have is that your include guards in you source files will cause all of the implementation to be skipped: 您遇到的问题是,源文件中的包含保护将导致跳过所有实现:

broken "game.cpp" : 损坏的“ 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

Second, the order in which you are including headers is incorrect. 其次,包含标题的顺序不正确。 I'm guessing you have something like this: 我猜你有这样的事情:

"game.h" “ game.h”

#ifndef GAME
#define GAME

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

#endif

"date.h" “ date.h”

#ifndef GAME
#define GAME

class Date
{
};

#endif

"game.cpp" “ game.cpp”

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

To fix this either swap the order of the includes in "game.cpp": 要解决此问题,请交换“ game.cpp”中包含的顺序:

"game.cpp" “ game.cpp”

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

Or include "date.h" in "game.h": 或在“ game.h”中包含“ date.h”:

"game.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" “ game.cpp”

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

您不需要C ++文件中的保护措施

The error is that you should use include guards only in your header files 错误是您仅应在头文件中使用包含防护

If you have a 如果你有一个

BaseClass.h
struct HumanEntity { }

and then have different classes use that base class, without the include guard you may run into issues. 然后让不同的类使用该基类,而没有包含保护,您可能会遇到问题。 That is why you put a 这就是为什么你放一个

#ifndef __MYGUARD
#define __MYGUARD

... // here is your header code

#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM