类变量导致段错误

时间:2013-10-25 07:02:48

标签: c++

我正在使用其他人在我的小组中创建的类,我注意到reduceStamina正在提供段错误。任何时候_staminaBar都使用该程序只是段错误。我也试过得到演员的名字,这也引起了一个段错误。我检查的第一件事是,如果调用默认构造函数,则不会调用它。 (我曾经在默认构造函数中有一个cout)然后我检查了变量是否在其他构造函数中初始化,并且它是。

为什么_reduceStamina导致段错误? 我的程序使其成为“In 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

1 个答案:

答案 0 :(得分:1)

在您的代码示例中看不到崩溃的原因。可能发生的是在非现有对象上调用该函数。

考虑以下代码:

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

那将以你所看到的方式失败。可能原因并不像这里那么简单,但你需要查看调用reduceStamina方法的回溯,因为它没有有效的Actor对象。