如何将#include“File1”包含到“File2”和“File3”中? C ++

时间:2014-03-18 18:42:03

标签: c++ class header

C ++ Codeblocks有帮助! 好吧,我不知道如何解释这个问题,然后我只是在问题的名字中做了, 但我希望你明白这个主意。如果没有,请不要担心,当您看到此代码时,您将会这样做:

//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游戏,以便于调试)

我只是想知道如何将分离的类文件包含到其他分离的类文件中,因为我在遇到问题后搜索了HOURS,但我什么也没找到。

现在出现了错误的部分: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) ===|

1 个答案:

答案 0 :(得分:0)

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

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