Cal*_*ght 0 c++ compiler-errors g++ include 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"
Run Code Online (Sandbox Code Playgroud)
team.h
#ifndef __game_h_
#define __game_h_
#endif
Run Code Online (Sandbox Code Playgroud)
team.cpp
#include "team.h"
#ifndef __game_h_
#define __game_h_
#endif
Run Code Online (Sandbox Code Playgroud)
game.h
#ifndef __date_h_
#define __date_h_
#endif
Run Code Online (Sandbox Code Playgroud)
game.cpp
#include "game.h"
#ifndef __date_h_
#define __date_h_
#endif
Run Code Online (Sandbox Code Playgroud)
date.cpp
#include "date.h"
Run Code Online (Sandbox Code Playgroud)
我使用Cygwin g ++编译器,我用来编译它的行是:
g++ football.cpp team.cpp game.cpp date.cpp -o football.exe
Run Code Online (Sandbox Code Playgroud)
以下是我得到的所有错误:(警告,文字墙)
$ 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
Run Code Online (Sandbox Code Playgroud)
如果需要进一步说明,将进行编辑,但我认为不会.
任何帮助将非常感激.
我认为你误解了包含警卫的使用.
#ifndef __game_h_
#define __game_h_
// ...
#endif
Run Code Online (Sandbox Code Playgroud)
上面的语句集通常仅用于描述游戏界面的头文件中,以防止多次包含该头文件.
但是在你的代码中,你已经在标题和实现中添加了包含保护,而且你似乎也混淆了实体 - 除非我误解了你的文件内容 - 例如在team.h中,你看起来似乎包含了防范游戏.
我的猜测是你滥用包含警卫实际上阻止了某些类型的定义.
正如其他人所提到的,不要使用以双下划线开头的名称.
例如,在团队标题中添加包含警戒以防止多个包含该文件,并为包含警卫提供反映他们正在守护的模块的名称:
// team.h
#ifndef TEAM_H
#define TEAM_H
// ... code for team.h in here
#endif // TEAM_H
Run Code Online (Sandbox Code Playgroud)