C++ C++;为类内的枚举项设置值
我的名字是乔希,我正在努力使这段代码工作,但我到目前为止什么也做不到。。。 对不起,如果这是愚蠢的,我在浪费你的时间,我是新的C++(我已经爱上了),让我试着解释。 基本上我想做的是在类中插入一个枚举,从枚举中将一个值设置为一个iten(可能是类),但我无法访问该项来设置值 战斗边缘-24点物理伤害/54点移动速度/5点能力。 对不起,英语不好,它不是我的主要语言C++ C++;为类内的枚举项设置值,c++,enums,C++,Enums,我的名字是乔希,我正在努力使这段代码工作,但我到目前为止什么也做不到。。。 对不起,如果这是愚蠢的,我在浪费你的时间,我是新的C++(我已经爱上了),让我试着解释。 基本上我想做的是在类中插入一个枚举,从枚举中将一个值设置为一个iten(可能是类),但我无法访问该项来设置值 战斗边缘-24点物理伤害/54点移动速度/5点能力。 对不起,英语不好,它不是我的主要语言 #include <iostream> using namespace std; class Enemy
#include <iostream>
using namespace std;
class Enemy {
protected:
int physicalDamage;
double moveSpeed;
int abilityPower;
public:
void setPhysicalDamage(int p) {
physicalDamage = p;
}
void setMoveSpeed(int m) {
moveSpeed = m;
}
void setAbilityPower(int ap) {
abilityPower = ap;
}
virtual void attack() = 0;
virtual void ability() = 0;
virtual void speed() = 0;
};
class Itens: public Enemy {
protected:
enum class itensID {
gunnersBreaker,
hitswamSword,
fineshotsWrath,
banesBook,
infinityStar,
edgeOfFight,
};
itensID iID;
public:
void setItensID(int r) {
iID.edgeOfFight = r; // Gives me error, I don't really know a way around that...
}; // But basically what I wanna do is marked in double slashes...
// I don't know if I should use pointers for that, but I tried and it also didn't work...
};
class Ninja : public Enemy {
public:
void attack() {
cout << "Hero: Ninja." << endl;
cout << "Physical Damage: " << physicalDamage << endl;
}
void ability() {
cout << "Ability Power: " << abilityPower << endl;
}
void speed() {
cout << "Movement Speed: " << moveSpeed << endl;
}
};
class Monster : public Enemy {
public:
void attack() {
cout << "Hero: Monster." << endl;
cout << "Physical Damage: " << physicalDamage << endl;
}
void ability() {
cout << "Ability Power: " << abilityPower << endl;
}
void speed() {
cout << "Movement Speed: " << moveSpeed << endl;
}
};
int main() {
Ninja n;
Monster m;
Enemy *e1 = &n;
Enemy *e2 = &m;
/*
Find a way to set an enum item to the Ninja hero
*/
e1->setPhysicalDamage(203);
e1->setAbilityPower(12);
e1->setMoveSpeed(321.32);
e2->setPhysicalDamage(40);
e2->setAbilityPower(654);
e2->setMoveSpeed(310.44);
n.attack();
n.ability();
n.speed();
m.attack();
m.ability();
m.speed();
}
#包括
使用名称空间std;
阶级敌人{
受保护的:
物理损伤;
双移动速度;
智力力量;
公众:
无效设置物理损坏(INTP){
物理损伤=p;
}
无效设置移动速度(整数米){
移动速度=m;
}
无效可设置功率(int ap){
能力功率=ap;
}
虚空攻击()=0;
虚空能力()=0;
虚空速度()=0;
};
第三类:公敌{
受保护的:
枚举类itensID{
炮手杀手,
希斯沃斯语,
好的,
班斯本,
无限星,
边角,
};
它属于iID;
公众:
void setItensID(int r){
iID.edgeOfFight=r;//给了我一个错误,我真的不知道如何解决这个问题。。。
};//但基本上我想做的是用双斜杠标记。。。
//我不知道我是否应该使用指针,但我试过了,它也不起作用。。。
};
忍者类:公敌{
公众:
无效攻击(){
cout这是因为您试图使用enum
像struct
一样,并将值赋给其中一个元素。enum
是命名值的列表,可用于表示一些不规则和/或不安全的值(如标志或状态),这些值通过算术类型表示。
你的itensID iID
是你的itensID
枚举类类型的变量,因此你可以将你在itensID
enum类主体中列出的一个命名值分配给它(即iID=itensID::edgeoffice
)。[主题外]为什么物品来自敌人?物品不应该是敌人。[主题内]为什么不将枚举设置为全局的,或者至少是公共的,这样就可以将枚举传递给函数而不是int
?[主题外]也许这是一个被诅咒的有情对象。算了吧,他在滚动。当与enum
一起使用时,class
似乎变得非常混乱,edgeOfFight
和其他枚举项都不是类字段,无法为它们分配任何内容。[在主题中]为什么不void setItensID(intensID r)
?谢谢!我将对代码进行一些更改,我必须检查一些结构表单才能使这些iTen正常工作,再次感谢您的帮助,我非常感谢!抱歉打扰您,我已经更新了代码…您能看一下吗?我需要一些帮助:(谢谢你的回答,我认为我有一个很好的基础来启动一个类似的程序,但似乎我还没有接近……顺便说一句,谢谢你的建议,我现在就要开始读这本书了!
#include <iostream>
#include <string>
using namespace std;
struct itensID {
void gunnersBreaker(int pd, int ap, float ms);
void hitswamSword(int pd, int ap, float ms);
void fineshotsWrath(int pd, int ap, float ms);
void banesBook(int pd, int ap, float ms);
void infinityStar(int pd, int ap, float ms);
void edgeOfFight(int pd, int ap, float ms);
};
void itensID::gunnersBreaker(int pd, int ap, float ms)
{
int physicalDamage = pd;
int abilityPower = ap;
float moveSpeed = ms;
cout << "\n Shotgun: Gunner's Break " << endl;
cout << "\n Stats:";
}
void itensID::hitswamSword(int pd, int ap, float ms)
{
}
void itensID::fineshotsWrath(int pd, int ap, float ms)
{
}
void itensID::banesBook(int pd, int ap, float ms)
{
}
void itensID::infinityStar(int pd, int ap, float ms)
{
}
void itensID::edgeOfFight(int pd, int ap, float ms)
{
}
class Enemy {
protected:
int physicalDamage;
int abilityPower;
float moveSpeed;
public:
void setEnemyStatus(int p, int a, float m) {
physicalDamage = p;
abilityPower = a;
moveSpeed = m;
}
virtual void attack() = 0;
virtual void ability() = 0;
virtual void speed() = 0;
};
// Basically what I wanna do now is, add two or three values to the items eg. gunnersBreaker (int pd, int ap & float ms)
// Since I am struggling to get this going, I have decided to set them as string, just to print while I figure-
// out how to add multiple values for a item in a structure, maybe I should make them a void function instead-
// of a regular variable? (I would discard this since regular variables takes only one identifier, I guess)
// or should I make it a class instead?
class Ninja : public Enemy {
public:
void attack() {
cout << "Hero: Ninja." << endl;
cout << "Physical Damage: " << physicalDamage << endl;
}
void ability() {
cout << "Ability Power: " << abilityPower << endl;
}
void speed() {
cout << "Movement Speed: " << moveSpeed << endl;
}
};
class Monster : public Enemy {
public:
void attack() {
cout << "Hero: Monster." << endl;
cout << "Physical Damage: " << physicalDamage << endl;
}
void ability() {
cout << "Ability Power: " << abilityPower << endl;
}
void speed() {
cout << "Movement Speed: " << moveSpeed << endl;
}
};
int main() {
Ninja n;
Monster m;
Enemy *e1 = &n;
Enemy *e2 = &m;
itensID iID;
/*
on the lines below ptr1 and ptr2, I got kind of an error,
I got to find a way to calculate the Enemy Status with the addition of the item specs
and print out the item I am using, EG. Hero: Ninja!
Weapon: Gunner's Breaker.
Stats.... and so on...
*/
e1->setEnemyStatus(35,10,310.21) + iID.gunnersBreaker(33,11,32.32); // ptr1
e2->setEnemyStatus(54,65,235.30); // ptr2
n.attack();
n.ability();
n.speed();
m.attack();
m.ability();
m.speed();
}