好的,所以我是一名 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
如果需要进一步澄清,将进行编辑,尽管我认为不会。
任何帮助将不胜感激。