C++ 如何使用C+中标题中声明的变量和函数+;
首先,这可能会很混乱,因为我对编程基本上是新手。C++ 如何使用C+中标题中声明的变量和函数+;,c++,header,C++,Header,首先,这可能会很混乱,因为我对编程基本上是新手。 我正在制作一个rpg游戏,我希望我所有的武器都在一个名为武器.cpp的文件中。 我这样做是为了在一个名为common.h的头文件中获得所有全局变量,如“weaponDamage”和“weaponName”,这样我就可以从main和.cpp文件中访问和操作这些变量。但问题是它似乎无法在我的标题中找到这些变量和函数 下面是一些代码: 普通。h: #include <string> #ifndef COMMON_H_INCLUDED
我正在制作一个rpg游戏,我希望我所有的武器都在一个名为武器.cpp的文件中。
我这样做是为了在一个名为common.h的头文件中获得所有全局变量,如“weaponDamage”和“weaponName”,这样我就可以从main和.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
#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";
}
是的,我确实记得包括了common.h现在我的IDE
code::Blocks
出现的错误是:error:common.h:没有这样的文件或目录我不知道为什么会这样,所以请帮忙 很抱歉发了这么长的帖子,请提前感谢。您应该使用
“common.h”
而不是
使用“common.h”
而不是
。
有角度的用于库文件。weopens.cpp大部分是无意义的 我尝试编译以下内容:
void a() {
int b = 5; }
int main()
{
a();
return 0;
}
g++不喜欢这个主意。你的“common.h”放在哪里?您必须从带有“arms.cpp”的文件夹中指定它们的相对路径
其次,在函数中定义局部变量。您的武器.cpp文件必须如下所示:
#include <iostream>
#include <string>
#include "relative/path/to/common.h"
//global variables
int pureDamage = 0;
int pureHealth = 0;
int weaponDamage;
int armorDefense;
int totalDamage = pureDamage + weaponDamage;
int totalHealth = pureHealth + armorDefense;
int totalLuck;
string starsign;
string weaponName;
using namespace std;
void weaponSwordIron()
{
weaponDamage = 5;
weaponName = "Iron Sword";
}
void weaponSwordGold()
{
weaponDamage = 8;
weaponName = "Gold Sword";
}
...
#包括
#包括
#包括“relative/path/to/common.h”
//全局变量
int pureDamage=0;
int pureHealth=0;
智力武器损害;
内铠装;
int总伤害=纯伤害+武器伤害;
int totalHealth=pureHealth+armorDefense;
int Totaluck;
弦星号;
字符串武器名称;
使用名称空间std;
无效武器
{
武器伤害=5;
武器名称=“铁剑”;
}
无效武器
{
武器伤害=8;
武器名称=“金剑”;
}
...
除了其他人提到的标题位置问题和局部变量阴影外,我认为您还将遇到多个定义错误的问题:
extern int pureDamage = 0;
此声明上的初始值设定项将覆盖外部
,因此此变量不仅将被声明,还将在包含此标头的任何编译单元中定义。你的链接器会抱怨的
如果确实要跨多个文件使用全局变量,则需要在源文件中定义和初始化变量一次,并在头文件中声明它
extern
(不带初始值设定项)<但是,代码>常量变量是一个例外。我认为作为一名新程序员,你最不想做的事情就是制作一个RPG。在我看来,它们需要比其他类型的游戏(从OOP开始)更健壮的设计。你需要编写#包括“common.h”
标题是否与cpp文件位于同一位置?我认为你应该避免全局变量。函数(如void weaponSwordIron(){int weaponDamage=5;string weaponName=“Iron Swarm”;}
初始化两个局部变量,这些变量在函数返回时会被销毁,从而使初始化变得毫无意义。这些函数实际上什么都不做。如果您从函数中删除了类型,那么您将修改全局变量-这至少使函数变得有用(尽管我对全局变量的使用有所保留)。common.h中的#include
应该在标题保护中(就在#define
之后).@LuchianGrigore:取决于编译器。这对符合标准的编译器不会有什么区别。@juanchopanza:可能会。尖括号表示“仅在系统定义的位置搜索”;引号的意思是“在其他地方搜索,然后在系统定义的位置搜索”。幸运的是,-I
选项算作系统定义的位置。尖括号禁止包含来自标题的警告。另外,我可以通过这种方式包含本地头。它也不喜欢在托管实现中使用void main
的想法。但是请听听其他人对设计的看法,因为这是一种非常糟糕的设计任何大小的程序的方法。
extern int pureDamage = 0;