首先,这可能很混乱,因为我对一般编程很陌生。
好吧,我正在制作一个 rpg 游戏,我希望我所有的武器都放在一个名为 Weapons.cpp 的文件中。
我这样做是为了在名为 common.h 的头文件中获取所有全局变量,例如“weaponDamage”和“weaponName”,这样我就可以从我的主文件和 .cpp 文件中访问和操作这些变量。但问题是它似乎无法在我的标题中找到这些变量和函数。
这是一些代码:
常见的.h:
#include <string>
#ifndef COMMON_H_INCLUDED
#define COMMON_H_INCLUDED
//global variables
extern int pureDamage = 0;
extern int pureHealth = 0;
extern int weaponDamage;
extern int armorDefense;
extern int totalDamage = pureDamage + weaponDamage;
extern int totalHealth = pureHealth + armorDefense;
extern int totalLuck;
extern string starsign;
extern string weaponName;
//all weapons
void weaponSwordIron();
void weaponSwordGold();
void weaponSwordSwordOfTheHeavens();
void weaponBowSimple();
void weaponBowLongBow();
void weaponBowThunder();
void weaponStaffStaffOfFlames();
void weaponStaffStaffOfLightning();
void weaponStaffStaffOfAssKicking();
#endif // COMMON_H_INCLUDED
武器.cpp:
#include <iostream>
#include <string>
#include <common.h>
using namespace std;
void weaponSwordIron()
{
int weaponDamage = 5;
string weaponName = "Iron Sword";
}
void weaponSwordGold()
{
int weaponDamage = 8;
string weaponName = "Gold Sword";
}
void weaponSwordSwordOfTheHeavens()
{
int weaponDamage = 15;
string weaponName = "Sword Of The Heavens";
}
void weaponBowSimple()
{
int weaponDamage = 5;
string weaponName = "Simple Bow";
}
void weaponBowLongBow()
{
int weaponDamage = 8;
string weaponName = "Long Bow";
}
void weaponBowThunder()
{
int weaponDamage = 15;
string weaponName = "Thunder Bow";
}
void weaponStaffStaffOfFlames()
{
int weaponDamage = 5;
string weaponName = "Staff Of Flames";
}
void weaponStaffStaffOfLightning()
{
int weaponDamage = 8;
string weaponName = "Staff Of Lightning";
}
void weaponStaffStaffOfAssKicking()
{
int weaponDamage = 15;
string weaponName = "Staff Of Ass Kicking";
}
和我的主要功能的一小部分,该功能称为GiveWeapon()
:
void GiveWeapon()
{
system("cls");
if (starsign == "mage")
{
weaponSwordIron();
cout << weaponDamage;
cout << weaponName;
}
else if (starsign == "warrior")
{
weaponBowSimple();
}
else if (starsign == "archer")
{
weaponStaffStaffOfFlames();
}
else
{
ChooseStarsign();
}
AssignAttributes();
}
是的,我确实记得包含 common.h
现在我的 IDECode::Blocks
出现的错误是:error: common.h: no such file or directory
我不知道为什么会出现这种情况,所以请帮忙。
抱歉发了这么长的帖子,并在此先感谢。