将枚举传递上课,然后返回
Pass enum to class and then return
我已经在Code.cpp中声明了以下枚举我使用的开关语句取决于枚举的设置为。
enum State { HighMoral, PoorMoral, EndGame };
State CurrentState = HighMoral;
switch (CurrentState)
{
case HighMoral: random = rand()%3+1;
switch (random)
{
case 1: CurrentState = g_Solider.KillCommander(&CurrentState, random = rand()%2+1);
break;
case 2: CurrentState = g_Solider.KillSeniorCommander(&CurrentState,random = rand()%2+1);
break;
case 3: CurrentState = g_Solider.LessHalfStrength(&CurrentState);
break;
};
break;
case PoorMoral: CurrentState = g_Solider.JoinSeniorCommander();
break;
};
我想将此枚举传递给类中的功能,然后返回HighMoral
,PoorMoral
或EndGame
,然后更改我的Switch语句的当前状态。
但是,当涉及到它并返回它时,我毫无头绪。我环顾四周,找不到找到方法的运气。
我有3个文件。code.cpp(包含 void main()
和 enum
),solider.h(包含solider类不知道状态枚举的存在(如何执行此操作?)),solider.cpp(包含所有SOLIDER代码,但需要采用当前状态和当前状态和返回新状态)
这是我想做的示例。
solider.h
#include <time.h>
#include <iostream>
using namespace std;
extern enum State;
class Solider
{
private:
public:
void KillSeniorCommander(State& currentState, int random); // Kill the Senior Commander or random event
void JoinSeniorCommander(State& currentState); // Have the Commander join the group
void DefunctGroup(State& currentState); // Disband the group
};
solider.cpp
void Solider::KillSeniorCommander(State& currentState, int random)
{
if (SeniorCommander==1) // If The SeniorCommander is Active
{
cout << "The Senior Commander has died!n";
SeniorCommander--; // Kill the SeniorCommander
Groupsize--; // Reduce the Groupsize
Strength = Strength - (5*2.5); // Remove SeniorCommanders impact on Strength
SquadMoral = SquadMoral - (5*2.5);// Remove SeniorCommanders impact on SquadMoral
CurrentState = PoorMoral;
}
else // Otherwise do something random
{
switch (random)
{
case 1: cout << "Your group survives a ambush!n";
break;
case 2: random = rand()%5+1; // Give random a new value
if (random>1)
{
cout << random << " group members have died!n"; // Kill x Aamount of members
}
else
{
cout << "A group member has died!n"; // Kill a member
}
Groupsize = Groupsize - random; // Remove the members from the group
Strength = Strength - (random*2.5); // Remove there effect Strength
SquadMoral = SquadMoral - (random*2.5); // Remove there effect on GroupMoral
break;
}
CurrentState = CurrentState;
}
} // KillSeniorCommander(int random)
void Solider::JoinSeniorCommander(State& currentState)
{
if (SeniorCommander==2 && Commander == 0) // Check to see if the Commander is dead and a
{ // SeniorCommander is not in service
cout << "The Senior Commander has joined!n";
SeniorCommander--; // Change their status to active
Groupsize++; // Add them to the group
Strength = Strength - (5*2.5); // Add their impact to Strength
SquadMoral = SquadMoral - (5*2.5); // Add their impact to GroupMoral
CurrentState = HighMoral;
}
else // He isn't available to join
{
cout << "You fail to recruit new command!n";
CurrentState = CurrentState;
}
} // JoinSeniorCommander()
void Solider::DefunctGroup(State& currentState)
{
cout << "Your group has been disbanded as it is not fit for duty.";
CurrentState = EndGame;
} // DefunctGroup()
code.cpp
在标题中定义枚举,并将其包含在两个文件中。
例如:
#ifndef SOLDIERSTATE_H
#define SOLDIERSTATE_H
enum SoldierState
{
HighMorale,
PoorMorale,
EndGame
};
#endif
枚举可以将其视为界面中的整数。您可以使用两种方法:通过参考并将函数更改为密集,或按值通过,然后按值返回下一个State
:
// one or the other
void nextState( State& currentState );
State nextState( State currentState );
与C 中的所有其他内容一样,您需要查看要使用的声明。在您的情况下,这意味着必须将State
的定义移至标题文件,然后将此文件包括在main.cpp和sersier.h中。然后,您将能够在Soldier
成员函数的声明中使用类型State
。
class Game
{
public:
enum State { HighMoral, PoorMoral, EndGame };
aMethod(State::PoorMoral);
};
Game::aMethod(State aState)
{
return State::HighMoral;
}
相关文章:
- 文字符号和字符串变量之间的串联然后返回常量字符*
- C - 将浮子转换为未签名的char数组,然后返回到浮点
- C 将字符串转换为char *,然后返回
- 通过将结构信息从二进制文件读取到数组中,然后返回到文件C
- 将光标更改为沙漏/等待/忙碌光标,然后返回Qt
- 将枚举传递上课,然后返回
- 打开一个 Shell 程序,向其传递参数,然后返回结果
- 从Istream中阅读,如何先读一个单词,然后读整行,然后返回
- 将短*/双*转换为整数*,然后返回并取消引用
- 将 cout 写入文件,然后返回终端
- 将字符串拆分为字符,然后返回字符串
- 将字符串转换为Unicode十六进制表示形式,然后返回C++
- 在函数中定义一个动态数组,然后返回数组元素。如何释放阵列的内存?
- 使用MPI临时接收数据,然后返回结果
- 将焦点设置到父窗口,然后返回到子窗口
- 删除它,然后返回创建的对象
- 用数字填充数组,然后返回指向该数组的指针
- 提升精神:如何将值对字符串解析为map<string,string>然后返回?
- C++跟踪一个项目一次,然后返回
- 计算平均值,然后返回数组中最接近平均值的元素