2013-10-25 46 views
-2

我正在使用其他人在我的組中創建的類,並且我注意到reduceStamina正在給出段錯誤。隨時可以使用_staminaBar程序只是段錯誤。我也嘗試過得到演員的名字,並且也導致了seg故障。我檢查的第一件事是如果默認構造函數被調用,它不被調用。 (我曾經在默認的構造函數中有一個cout)然後我檢查變量是否在其他構造函數中初始化,並且是。類變量導致段錯誤

爲什麼_reduceStamina會導致段錯誤? 我的程序,使得它「在reduceStamina」

#ifndef ACTOR_H_ 
#define ACTOR_H_ 

#include <iostream> 
#include "../resources/vector3d.h" 
using namespace std; 

namespace bammm 
{ 
    class Actor 
    { 

     private: 
      float _rotation; 
      string _name; 
      Vector3D *velocity; 
      Vector3D *location; 

      int _healthBar; 
      int _staminaBar; 
      int MAX_HEALTH; 
      int MAX_STAMINA; 

      int _attack; 
      int _defense; 

     public: 
      //Constructors 
      Actor(); 
      Actor(string myName); 
      Actor(string myName,int health,int stamina,int atck, int def); 

      //Functions 
      /* 
       setRotation 
       @Pre-Condition- takes no arguments 
       @Post-Condition- returns rotation 
      */ 
      void setRotation(float myRotation); 
      /* 
       setName 
       @Pre-Condition- takes no arguments 
       @Post-Condition- returns name 
      */ 
      void setName(string myName); 
      /* 
       increaseHealth 
       @Pre-Condition- Takes an float amount 
       @Post-Condition- Increases health by said amount 
      */ 
      void increaseHealth(int amount); 
      /* 
       increaseStamina 
       @Pre-Condition- Takes an float amount 
       @Post-Condition- Increases stamina by said amount 
      */ 
      void increaseStamina(int amount); 
      /* 
       reduceHealth 
       @Pre-Condition- Takes an float amount 
       @Post-Condition- Decreases health by said amount 
      */ 
      void reduceHealth(int amount); 
      /* 
       reduceHealth 
       @Pre-Condition- Takes an float amount 
       @Post-Condition- Decreases stamina by said amount 
      */ 
      void reduceStamina(int amount); 
      /* 
       isFullyRested 
       @Pre-Condition- no parameters 
       @Post-Condition- returns true if healthBar and staminaBar are equal to        MAX_HEALTH and MAX_STAMINA 
           returns false otherwise; 
      */ 
      bool isFullyRested(); 

      inline float getRotation() 
      { 
       return _rotation; 
      } 

      inline string getName() 
      { 
       return _name; 
      } 

      inline string getVelocity() 
      { 
       return velocity->toString(); 
      } 

      inline string getLocation() 
      { 
       return location->toString(); 
      } 

      inline int getHealth() 
      { 
       return _healthBar; 
      } 

      inline int getStamina() 
      { 
       return _staminaBar; 
      } 

      inline int getAttack() 
      { 
       return _attack; 
      } 

      inline int getDefense() 
      { 
       return _defense; 
      } 



    }; 

    Actor::Actor() 
    { 
    } 

    Actor::Actor(string myName) 
    { 
     _name = myName; 
     _rotation = 0; 
     velocity = new Vector3D(); 
     location = new Vector3D(); 

     MAX_HEALTH = 100; 
     MAX_STAMINA = 50; 
     _healthBar = MAX_HEALTH; 
     _staminaBar = MAX_STAMINA; 
     _healthBar = 100; 
     _staminaBar = 50; 
     _attack = 4; 
     _defense = 2; 
    } 

    Actor::Actor(string myName,int health,int stamina,int atck, int def) 
    { 
     _name = myName; 
     _rotation = 0; 
     velocity = new Vector3D(); 
     location = new Vector3D(); 

     MAX_HEALTH = health; 
     MAX_STAMINA = stamina; 
     _healthBar = MAX_HEALTH; 
     _staminaBar = MAX_STAMINA; 
     _healthBar = health; 
     _staminaBar = stamina; 
     _attack = atck; 
     _defense = def; 
    } 

    void Actor::setRotation(float myRotation) 
    { 
     _rotation = myRotation; 
    } 


    void Actor::setName(string myName) 
    { 
     _name = myName; 
    } 


    void Actor::increaseHealth(int amount) 
    { 
     if (_healthBar>=MAX_HEALTH) 
     { 
      _healthBar = MAX_HEALTH; 
      return; 
     } 
     else 
     { 
      _healthBar += amount; 
     } 
    } 


    void Actor::increaseStamina(int amount) 
    { 
     if (_staminaBar>=MAX_STAMINA) 
     { 
      _staminaBar = MAX_STAMINA; 
      return; 
     } 
     else 
     { 
      _staminaBar += amount; 
     } 
    } 


    void Actor::reduceHealth(int amount) 
    { 
     if (_healthBar > 0) 
     { 
      _healthBar -= amount; 
     } 
     else 
     { 
      _healthBar = 0; 
      return; 
     } 
    } 


    void Actor::reduceStamina(int amount) 
    { 
     cout << "In reduceStamina" << "\n"; 
     cout << "_stamina: " << _staminaBar << "\n"; 
     if (_staminaBar > 0) 
     { 
      _staminaBar -= amount; 
     } 
     else 
     { 
      _staminaBar = 0; 
     } 
    } 


    bool Actor::isFullyRested() 
    { 
     if (_healthBar == MAX_HEALTH && _staminaBar == MAX_STAMINA) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
} 
#endif 
+0

你如何實例化你的演員? – foobar123foofoo

+0

它正在與Actor :: Actor(string myName) – Taztingo

+0

實例化你爲什麼使用指針來分配向量? – Manu343726

回答

1

崩潰的原因是不是你的代碼示例中可見。可能發生的事情是該函數正在一個不存在的對象上被調用。

考慮以下代碼:

int foo; 
Actor * bad_actor = 0; // No Actor object exists, only a null pointer. 
bad_actor->reduceStamina(4); 

就像你看到這將在某種程度上失效。可能原因並不像這裏那麼簡單,但是您需要查看reduceStamina方法的回溯,因爲它沒有有效的Actor對象。

+0

如果演員無效,它仍然會進入該功能? – Taztingo

+0

這是問題,謝謝。我很驚訝NULL對象仍然會調用它的方法。 – Taztingo

+0

@ user1932934是的,在調用它的方法之前沒有對對象進行測試,以避免影響執行速度。 – vipw