如何使用单独的类将“File1”包含到“File2”和“File3”?C++

如何使用单独的类将“File1”包含到“File2”和“File3”?C++,c++,class,header,C++,Class,Header,C++代码块帮助! 好吧,我不知道该怎么解释,我只是以问题的名义, 但我希望你能明白。如果没有,请不要担心,当您看到以下代码时: //File1 (NOT main.cpp) #ifndef FILE1_H #define FILE1_H class File1 { doSomethingPlease(); }; #endif //File2 (NOT main.cpp) #ifndef FILE2_H #define FILE2_H #include "File1.h" Fi

C++代码块帮助! 好吧,我不知道该怎么解释,我只是以问题的名义, 但我希望你能明白。如果没有,请不要担心,当您看到以下代码时:

//File1 (NOT main.cpp)
#ifndef FILE1_H
#define FILE1_H
class File1
{
     doSomethingPlease();
};

#endif


//File2 (NOT main.cpp)
#ifndef FILE2_H
#define FILE2_H

#include "File1.h"
File1 f1; //This is the error part.
#endif
我不认为我可以更具体,不张贴我的意大利面代码的文本RPG游戏有许多单独的类文件,以便于调试

我只是想知道如何将分离的类文件包含到其他分离的类文件中,因为在我的问题出现后,我已经搜索了几个小时,但什么也没有找到

现在出现错误部分:E:\Codeblocks Projects\Dungeons&Orcs\Battle.cpp | 99 |对'quit::quitGame'的未定义引用| 我想你不想让我发布实际的代码,因为除了我,它几乎是不可读的

我遇到的另一个问题(由相同的代码引起)是前面提到的,但我可能在不知道File1的多个定义(第一个在File2.cpp中,第二个在File3.cpp中)的情况下修复了它

ERROR CODES: obj\Debug\Battle.o||In function `Z6battlev':|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|99|undefined reference to `quit::quitGame()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|150|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|170|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|182|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|233|undefined reference to `quit::quitGame()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|281|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|296|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|308|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|316|undefined reference to `Mainmenu::~Mainmenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|316|undefined reference to `Levelup::~Levelup()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|316|undefined reference to `Mainmenu::~Mainmenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|316|undefined reference to `Levelup::~Levelup()'|
obj\Debug\Battle.o||In function `ZN6BattleC2Ev':|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|319|undefined reference to `vtable for Battle'|
E:\Codeblocks Projects\Dungeons & Orcs\Battle.cpp|321|undefined reference to `Battle::battle()'|
obj\Debug\Forest.o||In function `Z6forestv':|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|42|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|49|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|56|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|68|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|75|undefined reference to `Battle::battle()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|83|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|85|undefined reference to `Battle::~Battle()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|85|undefined reference to `Mainmenu::~Mainmenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|85|undefined reference to `Levelup::~Levelup()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|85|undefined reference to `Battle::~Battle()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|85|undefined reference to `Mainmenu::~Mainmenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|85|undefined reference to `Levelup::~Levelup()'|
obj\Debug\Forest.o||In function `ZN6ForestC2Ev':|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|88|undefined reference to `vtable for Forest'|
E:\Codeblocks Projects\Dungeons & Orcs\Forest.cpp|90|undefined reference to `Forest::forest()'|
obj\Debug\Levelup.o||In function `Z7levelUpv':|
E:\Codeblocks Projects\Dungeons & Orcs\Levelup.cpp|75|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Levelup.cpp|76|undefined reference to `Mainmenu::~Mainmenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\Levelup.cpp|76|undefined reference to `Mainmenu::~Mainmenu()'|
obj\Debug\Levelup.o||In function `ZN7LevelupC2Ev':|
E:\Codeblocks Projects\Dungeons & Orcs\Levelup.cpp|80|undefined reference to `vtable for Levelup'|
E:\Codeblocks Projects\Dungeons & Orcs\Levelup.cpp|82|undefined reference to `Levelup::levelUp()'|
obj\Debug\main.o||In function `main':|
E:\Codeblocks Projects\Dungeons & Orcs\main.cpp|43|undefined reference to `Mainmenu::mainMenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\main.cpp|43|undefined reference to `Mainmenu::~Mainmenu()'|
E:\Codeblocks Projects\Dungeons & Orcs\main.cpp|43|undefined reference to `Mainmenu::~Mainmenu()'|
obj\Debug\Mainmenu.o||In function `Z8mainMenuv':|
E:\Codeblocks Projects\Dungeons & Orcs\Mainmenu.cpp|36|undefined reference to `Levelup::levelUp()'|
E:\Codeblocks Projects\Dungeons & Orcs\Mainmenu.cpp|49|undefined reference to `quit::quitGame()'|
E:\Codeblocks Projects\Dungeons & Orcs\Mainmenu.cpp|88|undefined reference to `Forest::forest()'|
E:\Codeblocks Projects\Dungeons & Orcs\Mainmenu.cpp|90|undefined reference to `Levelup::~Levelup()'|
E:\Codeblocks Projects\Dungeons & Orcs\Mainmenu.cpp|90|undefined reference to `Forest::~Forest()'|
E:\Codeblocks Projects\Dungeons & Orcs\Mainmenu.cpp|90|undefined reference to `Levelup::~Levelup()'|
E:\Codeblocks Projects\Dungeons & Orcs\Mainmenu.cpp|90|undefined reference to `Forest::~Forest()'|
obj\Debug\Mainmenu.o||In function `ZN8MainmenuC2Ev':|
E:\Codeblocks Projects\Dungeons & Orcs\Mainmenu.cpp|93|undefined reference to `vtable for Mainmenu'|
E:\Codeblocks Projects\Dungeons & Orcs\Mainmenu.cpp|95|undefined reference to `Mainmenu::mainMenu()'|
obj\Debug\quit.o||In function `ZN4quitC2Ev':|
E:\Codeblocks Projects\Dungeons & Orcs\quit.cpp|13|undefined reference to `quit::quitGame()'|
||=== Build finished: 46 errors, 0 warnings (0 minutes, 0 seconds) ===|

看起来您正在尝试包含要定义的头文件。如果您试图实现从其他头文件的继承,则仅应在头文件中包含include。例如:

#include "File1.h"
class File2 : public File1
{
};

我想我和你一样困惑。你发布的代码中哪里有quit::quitGame?你说的那行代码怎么会产生错误,实际上是产生错误的?不要使用全局变量。不要在头文件中定义违反一个定义规则的变量。谢谢大家的回答!顺便问一下,你是怎么找到我的帖子的?给我留下深刻印象的是,我这么快就得到了答案。无论如何,回到问题上来。问题是我使用了多个分开的类文件,它们都相互依赖才能工作,作为一个相关的例子:我有一个类Battle.cpp,它依赖于Player.cpp,因为Player.health必须包含在Battle.cpp中,所以我使用Player.cpp告诉Player.health-=orc.damage;