C++ 将枚举传递给类,然后返回

C++ 将枚举传递给类,然后返回,c++,class,function,enums,C++,Class,Function,Enums,我在code.cpp中声明了以下枚举 我使用的switch语句取决于枚举的设置 enum State { HighMoral, PoorMoral, EndGame }; State CurrentState = HighMoral; switch (CurrentState) { case HighMoral: random = rand()%3+1; switch (random)

我在code.cpp中声明了以下枚举 我使用的switch语句取决于枚举的设置

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类不知道状态enum是否存在(如何执行此操作?)、solider.cpp(包含所有solider代码,但需要采用当前状态并返回新状态)

这是一个我正在尝试做的例子

士兵

#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
};
#包括
#包括
使用名称空间std;
外部枚举状态;
阶级士兵
{
私人:
公众:
void KillSeniorCommander(State¤tState,int random);//杀死高级指挥官或随机事件
void JoinSeniorCommander(State和currentState);//让指挥官加入该组
void DefunctGroup(State¤tState);//解散该组
};
士兵

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()
void Solider::killseniordcommander(State¤tState,int-random)
{
if(SeniorCommander==1)//如果SeniorCommander处于活动状态
{

cout枚举可以在接口中被视为整数。您可以使用两种方法中的一种:按引用传递并让函数对其进行内部更改,或按值传递并按值返回下一个
状态:

// one or the other
void nextState( State& currentState );
State nextState( State currentState );

和C++中的其他所有一样,你需要看到你想要使用的声明。在你的例子中,这意味着“代码>状态<代码>必须被移动到一个头文件,这个文件被包含在Meav.CPP和Prave.H.中,你将能够在<代码>士兵< /代码声明中使用类型<代码>状态< /代码>。>成员功能正常。

在头中定义枚举,并将该头包含在两个文件中

例如:

#ifndef SOLDIERSTATE_H
#define SOLDIERSTATE_H

enum SoldierState
{
    HighMorale, 
    PoorMorale, 
    EndGame
};

#endif

State MyFunction(State inp){…return HighMoral;..}
有什么问题吗?您的要求不是很清楚。您希望在一个函数中传递新状态并返回旧状态?这将是
State setState(State newState){State temp=currentState;currentState=newState;return temp;}我刚刚更新了这个问题,试图更好地解释我打算做的事情。在C++中,成员获得包含枚举的范围,所以说<代码>状态::HuangMeals<代码>是不正确的。你可以通过C++来引用C++ 11中的这个行为。但是,我已经用这个代码更新了这个问题——我仍然有点被卡住了:(安迪):代码显示了很多缺陷。我鼓励你选一本好的C++书籍。我真的不知道从哪里开始:引用,函数签名(什么是返回的或不返回的)…基本上在“无效”()中。'我正在运行依赖于状态的'switch语句'。我希望'class functions'将该'state'更改为新的:).@Andy:
void main
是一个错误,返回类型必须是
int
。这就是为什么我建议使用初学者手册。在你的代码中,似乎每个对象都有一个状态加上你传入的状态,这就是你想要的状态?还有一些其他注意事项,你不能从返回
void
的函数中赋值……这和David的answer帮我修好了-现在可以用了-谢谢:)!
#ifndef SOLDIERSTATE_H
#define SOLDIERSTATE_H

enum SoldierState
{
    HighMorale, 
    PoorMorale, 
    EndGame
};

#endif