2012-11-05 76 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類型。

相關問題