2013-04-11 48 views
0

下面是頁眉和cpp文件我使用可以訪問頂級變量和方法在不同的類

#include "2d/Vector2D.h" 
#include <list> 
#include "../../AbstTargetingSystem.h" 


class AbstRaven_Bot; 




class Fletcher_getClosestBotStrategy 
{ 

public: 
    Fletcher_getClosestBotStrategy() 
    {} 
    Fletcher_getClosestBotStrategy(AbstRaven_Bot* owner); 

    //each time this method is called the opponents in the owner's sensory 
    //memory are examined and the closest is assigned to m_pCurrentTarget. 
    //if there are no opponents that have had their memory records updated 
    //within the memory span of the owner then the current target is set 
    //to null 

    void  pickTarget(); 
}; 
class Fletcher_TargetingSystem : public AbstTargetingSystem 
{ 

public: 
    Fletcher_TargetingSystem(AbstRaven_Bot* owner); 

    //each time this method is called the opponents in the owner's sensory 
    //memory are examined and the closest is assigned to m_pCurrentTarget. 
    //if there are no opponents that have had their memory records updated 
    //within the memory span of the owner then the current target is set 
    //to null 
    void  Update(); 
    void   closestBotStrategy(); 

    void  setCurrentTarget(AbstRaven_Bot* t); 
    AbstRaven_Bot* getCurrentTarget(); 

    void  setCurrentOwner(AbstRaven_Bot* t); 
    AbstRaven_Bot* getCurrentOwner(); 
}; 

#endif 

CPP

#include "Fletcher_TargetingSystem.h" 
#include "../../AbstRaven_Bot.h" 
#include "../../Raven_SensoryMemory.h" 
#include "../../Debug/DebugConsole.h" 


//-------------------------------- ctor --------------------------------------- 
//----------------------------------------------------------------------------- 
Fletcher_TargetingSystem::Fletcher_TargetingSystem(AbstRaven_Bot* owner): 
AbstTargetingSystem(owner){ 

} 

//-------------------------------- ctor --------------------------------------- 
//----------------------------------------------------------------------------- 
Fletcher_getClosestBotStrategy::Fletcher_getClosestBotStrategy(AbstRaven_Bot* owner){ 

} 

//----------------------------- Update ---------------------------------------- 

//----------------------------------------------------------------------------- 

//#define script AbstTargetingSystem::Instance() 
//class AbstTargetingSystem; 
//----------------------------- closestPlayer ---------------------------------------- 

//----------------------------------------------------------------------------- 
void Fletcher_getClosestBotStrategy::pickTarget() 
{ 

    double ClosestDistSoFar = MaxDouble; 


    for (curBot; curBot != SensedBots.end(); ++curBot) 
    { 
     //make sure the bot is alive and that it is not the owner 
     if ((*curBot)->isAlive() && (*curBot != getCurrentOwner())) 
     { 
      double dist = Vec2DDistanceSq((*curBot)->Pos(), getCurrentOwner()->Pos()); 
      if (dist < ClosestDistSoFar) 
      { 
       ClosestDistSoFar = dist; 
       setCurrentTarget(*curBot);// = *curBot; 
      } 

     } 
    } 

} 
void Fletcher_TargetingSystem::Update() 
{ 
    // currentStrategy = targetClosestBotStrategy; 
    // target = currentStrategy.pickTarget(); 
    std::list<AbstRaven_Bot*> SensedBots; 
    SensedBots = getCurrentOwner()->GetSensoryMem()->GetListOfRecentlySensedOpponents(); 

    std::list<AbstRaven_Bot*>::const_iterator curBot = SensedBots.begin(); 
    setCurrentTarget(0);//  = 0; 

    Fletcher_getClosestBotStrategy* fGCBS = new Fletcher_getClosestBotStrategy(this->getCurrentOwner()); 
    fGCBS->pickTarget(); 
} 

AbstRaven_Bot* Fletcher_TargetingSystem::getCurrentOwner(){ 
    return m_pOwner; 
} 
AbstRaven_Bot* Fletcher_TargetingSystem::getCurrentTarget(){ 
    return m_pCurrentTarget; 
} 

void Fletcher_TargetingSystem::setCurrentTarget(AbstRaven_Bot* t){ 
    m_pCurrentTarget = t; 
} 
void Fletcher_TargetingSystem::setCurrentOwner(AbstRaven_Bot* t){ 
    m_pOwner = m_pOwner; 
} 

void Fletcher_getClosestBotStrategy::pickTarget()我想訪問更新void Fletcher_TargetingSystem::Update()中的curBot和SensedBot方法。我也希望能夠訪問void Fletcher_TargetingSystem::setCurrentOwner(AbstRaven_Bot* t)和其他3類似於dat

任何幫助表示讚賞。

回答

1

從您的方法Fletcher_getClosestBotStrategy::pickTarget()您不能訪問Fletcher_TargetingSystem::Update()變量curBotSensedBot,因爲它們是局部變量。

所以,如果你真的需要訪問它們,將它們轉換成Fletcher_TargetingSystem類屬性,同時創建getter和setter方法,並試圖避免friend clases,重新defininig類的依賴關係:通過一個Fletcher_TargetingSystem常量引用您的Fletcher_getClosestBotStrategy方法:

例如:

class Fletcher_getClosestBotStrategy 
{ 
    void pickTarget(const Fletcher_TargetingSystem & targeting); 
    .... 
}; 

.... 

class Fletcher_TargetingSystem 
{ 
    protected: 
     std::list<AbstRaven_Bot*> SensedBots; 
    public: 
     std::list<AbstRaven_Bot*> & getSensedBots() const { return SensedBots; } 
}; 

void Fletcher_getClosestBotStrategy::pickTarget(const Fletcher_TargetingSystem & targeting) 
{ 
    double ClosestDistSoFar = MaxDouble; 
    std::list<AbstRaven_Bot*> & myLocalSensedBots = targeting.getSensedBots(); 
    ... 
} 
+0

當然,這給出錯誤,它是一種解釋,而不是真正的代碼。如果您需要訪問'Fletcher_TargetingSystem'公共方法,請按照我的說法將'Fletcher_TargetingSystem'對象的有效引用傳遞給'Fletcher_getClosestBotStrategy :: pickTarget()'。但是,正如我寫的,因爲是局部變量,您將永遠無法訪問'Fletcher_TargetingSystem :: Update()'之外的'curBot'和'SensedBots'。我希望這個幫助。 – 2013-04-11 13:45:23

+0

用新代碼編輯你的問題,我想你忘了班級名稱:必須是'void Fletcher_getClosestBotStrategy :: pickTarget(Fletcher_TargetingSystem&targeting)' – 2013-04-11 14:05:49

+0

從哪裏你需要訪問? – 2013-04-11 14:16:52

相關問題