【问题标题】:Pass enum to class and then return将枚举传递给类,然后返回
【发布时间】:2012-11-05 12:41:41
【问题描述】:

我在 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(包含solder类不知道状态枚举存在(怎么做?)),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

【问题讨论】:

  • State MyFunction(State inp) { ... return HighMoral; ... } 有什么问题?
  • 您的要求不是很清楚。您想在一个函数中传递新状态并返回旧状态吗?那将是State setState(State newState) { State temp = currentState; currentState = newState; return temp; }
  • 我刚刚更新了这个问题,试图更好地解释我打算做什么。

标签: c++ class function enums


【解决方案1】:

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

例如:

#ifndef SOLDIERSTATE_H
#define SOLDIERSTATE_H

enum SoldierState
{
    HighMorale, 
    PoorMorale, 
    EndGame
};

#endif

【讨论】:

  • 这和大卫的回答帮助我解决了它 - 现在可以工作了 - 谢谢 :)!
【解决方案2】:

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

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

【讨论】:

  • 我刚刚用这段代码更新了这个问题 - 我仍然有点卡住:(。
  • @Andy:代码显示了很多缺陷。我鼓励你选择一本好的 C++ 书籍。我真的不知道从哪里开始:引用、函数签名(返回或不返回什么)......
  • 基本上在'void main()'中运行'switch语句',这取决于状态是什么我希望'类函数'将'状态'更改为新状态:) .
  • @Andy:void main 是一个错误,返回类型必须是int。这就是为什么我推荐一本入门书。在您的代码中,似乎每个对象都有一个状态加上您传入的状态,这就是您想要的状态?其他一些注意事项,您不能从返回 void...的函数中分配...
【解决方案3】:

与 C++ 中的其他所有内容一样,您需要查看要使用的内容的声明。在您的情况下,这意味着必须将State 的定义移动到头文件中,然后该文件同时包含在 main.cpp 和士兵.h 中。然后,你就可以在Soldier成员函数的声明中正常使用State类型了。

【讨论】:

    【解决方案4】:
    class Game
    {
        public:
            enum State { HighMoral, PoorMoral, EndGame };
            aMethod(State::PoorMoral);
    };
    
    Game::aMethod(State aState)
    {
        return State::HighMoral;
    }
    

    【讨论】:

    • 在 C++ 中枚举成员获取包含枚举的范围,所以说State::HighMoral 是不正确的。您可以通过说enum class State {}; 在 c++11 中获得这种行为
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多