2012-11-05 79 views
0

我已经在code.cpp 中声明了以下enum,并且使用了一个switch语句,该语句取决于enum的设置。将enum传递给class然后返回

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

+2

出了什么问题'国家的MyFunction(州INP){...返回HighMoral; ''? – tenfour

+0

您的要求不是很清楚。你想传递新的状态并返回一个函数中的旧状态?那将会是'State setState(State newState){State temp = currentState; currentState = newState;返回温度; }' – john

+0

我刚刚更新了这个问题,试着给我更好的解释我打算做的事情。 – Andy

回答

0

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

例如:

#ifndef SOLDIERSTATE_H 
#define SOLDIERSTATE_H 

enum SoldierState 
{ 
    HighMorale, 
    PoorMorale, 
    EndGame 
}; 

#endif 
+0

这和David的答案帮助我修复了它 - 它现在可以工作 - 谢谢:)! – Andy

0

枚举可以被视为在接口整数。您可以在两种方法使用:通过引用传递和intenally具备的功能变化,或按值传递和返回值下一State

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

我刚刚更新了这个代码的问题,但我仍然有点卡住:(。 – Andy

+0

@安迪:代码显示了不少缺点,我鼓励你选择一本好的C++书籍,我不知道在哪里开始:引用,函数签名(什么是返回或不)... –

+0

基本上在'无效main()'即时通讯运行该'开关语句'这是依赖于什么状态是我想'类功能'改变那'状态'到一个新的:)。 – Andy

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

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

出了什么问题? – Azodious

+0

在C++中,枚举成员获取包含枚举的作用域,所以说'State :: HighMoral'将不正确。你可以在C++ 11中通过说enum class State {}来获得这种行为;' – luke

0

与一切用C其他++,你需要看到你想要使用的东西的声明。在你的情况下,这意味着State的定义必须移动到头文件,然后这个文件包含在main.cpp和soldier.h中。然后,您将可以在Soldier成员函数的声明中正常使用State类型。

相关问题