将枚举传递给类然后返回

时间:2012-11-05 12:41:41

标签: 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)
                        {
                            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;
};

我想将此枚举传递给类中的函数,然后让它返回HighMoralPoorMoralEndGame并更改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

4 个答案:

答案 0 :(得分:0)

枚举可以在接口中被视为整数。您可以使用以下两种方法:通过引用传递并使函数有意更改,或按值传递并按值返回下一个State

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

答案 1 :(得分:0)

与C ++中的其他内容一样,您需要查看要使用的内容的声明。在您的情况下,这意味着必须将State的定义移动到头文件中,然后将该文件包含在main.cpp和soldier.h中。然后,您将能够在State成员函数的声明中使用Soldier类型。

答案 2 :(得分:0)

在标题中定义枚举,并在两个文件中包含该标题。

例如:

#ifndef SOLDIERSTATE_H
#define SOLDIERSTATE_H

enum SoldierState
{
    HighMorale, 
    PoorMorale, 
    EndGame
};

#endif

答案 3 :(得分:-1)

class Game
{
    public:
        enum State { HighMoral, PoorMoral, EndGame };
        aMethod(State::PoorMoral);
};

Game::aMethod(State aState)
{
    return State::HighMoral;
}