C++为类中的枚举项设置值
C++ setting a value to a enum item inside a class
我的名字是乔希,我正在尝试使这段代码工作,但到目前为止我什么也做不了...... 对不起,如果这很愚蠢,我在浪费你的时间,我是C++新手(我已经喜欢了(,让我试着解释一下。
基本上我想做的是将枚举插入到类中,从枚举(也许是类(中为 iten 设置一个值,但我无法访问该项目来设置值......
例如。战斗边缘 - 24 物理伤害/54 移动速度/5 技能力量。 对不起,英语不好,这不是我的主要语言...
#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();
}
我已经更新了代码,但出现一个错误,我正在尝试修复它。我将解释我现在要做什么。
我希望问题通过添加枪手休息项目来更新英雄的当前 pd,ap,ms
更新的代码!
#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();
}
这是因为您尝试像struct
一样使用enum
并为其元素之一分配值。enums
是命名值的列表,可用于表示一些不方便和/或不安全的值(例如标志或状态(,这些值不方便和/或不安全。 您的itensID iID
是itensID
枚举类类型的变量,因此您可以为其分配您在枚举类主体中列出的itensID
命名值之一(即iID = itensID::edgeOfFight
(。
相关文章:
- 不带大括号的枚举形式
- C++为类中的枚举项设置值
- C++如何将字符串数组中的一个点设置为枚举变量
- 如何在枚举上设置范围?
- 使用索引设置矢量枚举时的奇怪行为
- C++:如何将枚举元素设置为无限?
- 设置类的枚举成员无效地使用"枚举"
- 是否有C++准则将第一个枚举设置为"safe"默认值?
- 如何将GLM :: VEC3(1.f,0.f,0.f)设置为枚举值
- 在类中获取并设置枚举变量
- 可以将枚举的默认类型设置为无符号字符
- 有没有一种方法可以为枚举设置运算符=
- 使用包含QProcess枚举的QString在QTextBrowser中设置文本
- 获取和设置C++中的枚举
- 不按递增顺序手动取消枚举和设置值
- 如何使用包含不同typedef枚举的派生类模板设置构造函数初始化列表
- cpp文件中设置静态枚举变量的正确方法
- 专用模板根据变量类型返回/设置枚举值
- 当试图添加类构造时,C++未设置枚举类型
- 在相同的命名空间但不同的类中为枚举设置值?C++