我正在努力制造一个"怪物发生器"对于我的控制台游戏,但遇到了一个可能是一个简单的问题需要解决,问题是我对这种语言有点新。
在我的main.cpp中,我创建了一个名为generateMonster()
的函数:
所有generateMonster()
所做的就是随机生成6个数字,这些数字将成为这个新怪物的属性。
generateMonster()
成功创建了一个具有随机属性的怪物,但为什么我不能通过main.cpp称这个新怪物?每当我运行我的构建时,它会说:
"错误:名称未在此范围内声明" ----如果我将其更改为Monster::name.returnHp();
,则表示"错误:名称不是' Monster'"的成员。但事实是,我刚刚创造了这个名字'与我的构造函数。
的main.cpp
#include <iostream>
#include <windows.h>
#include <time.h>
#include "Character.h"
#include "Monster.h"
using namespace std;
void generateMonster();
int main()
{
Character Bryan = Character(1,2,5,1,10,6);
int choice;
while(true)
{
cin >> choice;
if(choice == 1)
{
generateMonster();
cout << "Monster Stats:\n\n";
cout << "Life Points: " << name.returnHp() << endl ;
cout << "Endurance Chance: " << name.returnEndur() << endl ;
cout << "Dmg: " << name.returnDmg() << endl ;
cout << "Armor: " << name.returnArmor() << endl ;
cout << "Evasion Chance: " << name.returnEvasion() << endl ;
cout << "Agility Chance: " << name.returnAgility() << endl ;
}
}
return 0;
}
void generateMonster()
{
int random ;
int i, j, k, nums[50];
srand(time(NULL)) ;
for( i = 0; i < 50 ; i++)
{
nums[i] = i;
}
for (i = 0; i < 50; i++)
{
j = (rand() % 50) ;
k = nums[i] ; nums[i] = nums[j] ; nums[j] = k ; //switch rands
}
Monster name = Monster(nums[4],nums[45],nums[34],nums[21],nums[37],nums[41]);
//creates an object(Monster class) called name with the random attributes
}
Monster.h
#ifndef MONSTER_H_INCLUDED
#define MONSTER_H_INCLUDED
class Monster
{
private:
float c_hp;
float c_maxHp;
float c_endur;
float c_dmg;
float c_armor;
float c_evade;
float c_agility;
public:
Monster(int hp,int endur, int dmg, int armor, int evasion, int agility);
//constructor
void takeDmg(float dmg);
float returnHp();
float returnMaxHp();
float returnDmg();
float returnArmor();
float returnEndur();
float returnEvasion();
float returnAgility();
};
#endif // MONSTER_H_INCLUDED
Monster.cpp
#include "Monster.h"
#include <iostream>
Monster::Monster(int hp,int endur,int dmg,int armor,int evasion, int agility)
{
c_hp = hp * 50;
c_maxHp = hp * 50;
c_endur = endur * 3;
c_dmg = dmg * 2;
c_armor = armor * 2.5;
c_evade = evasion * 2;
c_agility = agility * 3;
}
void Monster::takeDmg(float dmg)
{
using std::cout;
float trueArmor,trueDmg;
trueArmor = c_armor / 1.60;
trueDmg = dmg - trueArmor;
if(c_hp - trueDmg <= 0)
{
cout << "You have slain the creature!";
c_hp = 0;
}
else
{
c_hp = c_hp - trueDmg;
}
}
//simple return functions
float Monster::returnHp(){return c_hp;}
float Monster::returnMaxHp(){return c_maxHp;}
float Monster::returnDmg(){return c_dmg;}
float Monster::returnArmor(){return c_armor;}
float Monster::returnEndur(){return c_endur;}
float Monster::returnEvasion(){return c_evade;}
float Monster::returnAgility(){return c_agility;}
答案 0 :(得分:0)
name
中不存在 main
。它只存在于generateMonster
函数内。要生成可以在main
中使用的怪物,只需从函数中返回它即可。将返回类型更改为Monster
,并添加一个return语句。
Monster generateMonster()
{
... Everything your function is currently doing ...
// plus this
return name;
}
然后,在main中,你可以这样做:
Monster m = generateMonster();