Thread: Need help with a class variable.

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Need help with a class variable.

    Here is one of my class's.

    Code:
    class Player {
          public:            
                 // Function to get private variables
                 void setPlayer();
                 void setStats();
                 void setExp();   
                 int setPlvl();  // Plvl = players current level
                 
          private:
                  int BaseStr;
                  int Strength;
                  int BaseMagic;
                  int Magic;
                  int BaseHealth;
                  int Health;
                  int EXP;
                  int Plvl;
                  
                  string Race;
                  string Type;
                  string Name;
                  };
    What I having problems thinking about is how I should declare Plvl (Player level) and EXP (expeirence). Both of these variables are going to have to be modified from outside of the class. Here is a hypothetical class, where the variables from the Player Class will be modified:

    Code:
    class Monster
    {
        public:
                 ...
                 ...
                 int getEXPgiven();
       private: 
                 int EXPgiven;
    }
    
    int Monster::getEXPgiven()
    {
    /*some formula*/ = EXPgiven;
    
    return EXPgiven;   // Not sure about this
    }
    Would it be best for me to just pass the EXPgiven variable from class Monster as a parameter to class Player::setExp ? Would sending the parameter to a function in another class work?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by RealityFusion
    Would it be best for me to just pass the EXPgiven variable from class Monster as a parameter to class Player::setExp?
    Yes.

    Quote Originally Posted by RealityFusion
    Would sending the parameter to a function in another class work?
    Yes again, assuming the function you are calling is public.

  3. #3
    1479
    Join Date
    Aug 2003
    Posts
    253
    When the program starts I want the exp and plvl variable to be 0. Should I state this in the class constructor or just set them to equal 0 in the private variable class list? I am worried if I do that, exp and plvl will always be 0.

    Code:
    class Player {
          public:            
                 // Function to get private variables
                 void setPlayer();
                 void setStats();
                 void setExp();   
                 int setPlvl();  // Plvl = players current level
                 
          private:
                  int BaseStr;
                  int Strength;
                  int BaseMagic;
                  int Magic;
                  int BaseHealth;
                  int Health;
                  int EXP = 0; // Like this or should I do this in the constructor?
                  int Plvl = 0;
                  
                  string Race;
                  string Type;
                  string Name;
                  };
    Knowledge is power and I want it all

    -0RealityFusion0-

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    In the constructor
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    1479
    Join Date
    Aug 2003
    Posts
    253

    More trouble.

    I am trying to pass the variable from class Player to a member function in class Monsters. I need to know how to acess member functions to another class from inside a different class memeber funciton. Here is what I am trying to do:

    Code:
    void Player::setExp(int XpGiven)
    {
        Exp = Exp + XpGiven;
    
        if (Exp == 100)
         {
            Plvl = 5;
         }
      
        M.MonsterStats(Plvl);  //Trying to acess the Monster class member funciton 
      }
    }
    This isn't correct I keep getting the error message 'M is undeclared'.
    Here is the part I am trying to send the variable to:
    Code:
    int Monsters::MonsterStats(int Clvl)  // Clvl = Plvl
    {
         int d;
         srand (time(0));
         d = rand() % 5;
         
         Mlevel = Clvl + rand() % 3;  // Clvl = Plvl
         
         MStr = Mlevel + 5;
         MAttackDamage = Mlevel * 2;
         MSpellDamage = Mlevel * 10;
         MonsterHP = Mlevel * 50;
         if (Mlevel == 0)
         {
             MonsterHP = 10;
             MAttackDamage = MStr + d;
             if (MAttackDamage == 0)
             {
               cout <<MName <<" missed!!" <<endl;
             }
         }
         
         ExpGiven = 10;
    }
    So, how am I supposed to acess Monsters::MonsterStats() from Player::setExp() so I can send variable Plvl to it?

    Note: FYI, in main I have Monsters M and Players P
    Knowledge is power and I want it all

    -0RealityFusion0-

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Should I state this in the class constructor or just set them to equal 0 in the private variable class list?
    Code:
     private:
                  int BaseStr;
                  int Strength;
                  int BaseMagic;
                  int Magic;
                  int BaseHealth;
                  int Health;
                  int EXP = 0; // Like this or should I do this in the constructor?
                  int Plvl = 0;
    You can't do that in C++. You can in Java.

    Code:
    void Player::setExp(int XpGiven)
    {
        Exp = Exp + XpGiven;
    
        if (Exp == 100)
         {
            Plvl = 5;
         }
      
        M.MonsterStats(Plvl);  //Trying to acess the Monster class member funciton 
      }
    }
    This isn't correct I keep getting the error message 'M is undeclared'.
    You have to learn a little more about something called "scope". Briefly, every variable has a certain "scope" or a block of code in your program in which the variable is visible. A function is like a separate program. The function can see variables that are declared inside it or that are passed as arguments to the function parameters. Additionally, in the case of a class function, the function can see the member variables of the class(that's because class functions are automatically passed an invisible parameter called "this", which is implicitly used to access the member variables). In your case, M is neither declared inside your function nor is it passed as an argument nor is it a member of the Player class.
    Last edited by 7stud; 10-19-2005 at 12:53 PM.

  7. #7
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by 7stud
    You can't do that in C++. You can in Java.


    You have to learn a little more about something called "scope". Briefly, every variable has a certain "scope" or a block of code in your program in which the variable is visible. A function is like a separate program. The function can see variables that are declared inside it or that are passed as arguments to the function parameters. Additionally, in the case of a class function, the function can see the member variables of the class(that's because class functions are automatically passed an invisible parameter called "this", which is implicitly used to access the member variables). In your case, M is neither declared inside your function nor is it passed as an argument nor is it a member of the Player class.
    I understand about scope, that is why I am having my problem. M is declared as this in main (along with Player):
    Code:
    int main()
    {
    Monsters M;
    Player P;
    
    ... // rest of code here
    
    }
    I know that I can't do the above code I listed because using M.whatever is not in scope of the Player class...which is why I am asking what I am asking. I need to pass a variable from class Player to class Monsters through the member functions...how do I do it?
    Knowledge is power and I want it all

    -0RealityFusion0-

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    If your trying to basically associate between 2 classes pointers might make that quite a bit easier. For example


    Assume all included and variables work etc.
    Code:
    class Owner{
    private:
    Pet* pet;
    public:
    Pet* getPet()const{return pet;}
    
    
    };
    
    class Pet{
    private:
    Owner*owner
    public:
    Owner* getOwner()const{return owner;}
    };
    As you can see with the pointer im writing the getter in the other class.

    I could also do something like this
    keep in mind this is the Owner.cpp file and I passed the
    Pet p by reference. That allows me to call functions from the Pet class as in P.setName(); because i created a Pet object p.
    Code:
    	void Owner::buyPet(Pet& p, const string name)
    	{
    		//gets the name of the pet and assigns it to the owners pet
    		p.setName(name);
    		pet = &p;
    		p.setOwner(this);
    	}
    These are just 2 examples, i think the second is more of what your looking for though.
    Last edited by gamer4life687; 10-19-2005 at 05:17 PM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  9. #9
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by gamer4life687
    If your trying to basically associate between 2 classes pointers might make that quite a bit easier. For example


    Assume all included and variables work etc.
    Code:
    class Owner{
    private:
    Pet* pet;
    public:
    Pet* getPet()const{return pet;}
    
    
    };
    
    class Pet{
    private:
    Owner*owner
    public:
    Owner* getOwner()const{return owner;}
    };
    As you can see with the pointer im writing the getter in the other class.
    I couldn't get this working because I kept getting this error

    Player is not a type
    Quote Originally Posted by gamer4life687
    I could also do something like this
    keep in mind this is the Owner.cpp file and I passed the
    Pet p by reference. That allows me to call functions from the Pet class as in P.setName(); because i created a Pet object p.
    Code:
    	void Owner::buyPet(Pet& p, const string name)
    	{
    		//gets the name of the pet and assigns it to the owners pet
    		p.setName(name);
    		pet = &p;
    		p.setOwner(this);
    	}
    These are just 2 examples, i think the second is more of what your looking for though.
    Got the same error when trying this.

    I think what I am needing is to use inheritence. I just need to get a variable from class Player so I can use it in class Monsters. I tried doing this:

    Code:
    class Monsters : private Player {
    To try and set up inheritence so I can get acess to the variable from class Player. I get this error....

    expected class-name before '{' token

    Will inheritance accomplish what I am trying to do, if so, how? All I need is a variable from class Player so I can use it in class Monsters.
    Last edited by RealityFusion; 10-20-2005 at 09:35 AM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Well your not going to get that code working b/c thats just a sample of a program i wrote. Its missing things of course. I was just showing an example of how to call functions from other classes. Of course you cant access the private variables from another class unless you use a getter. For inheritance been a while since i touched on that im sure some1 will fill u in on that.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  11. #11
    1479
    Join Date
    Aug 2003
    Posts
    253
    Anyone know why I am getting an error when I am trying to use inheritance.

    Code:
    class Monsters : public Player {
    keeps telling me that it expects a class-name before the '{' token, any help would be very useful.
    Knowledge is power and I want it all

    -0RealityFusion0-

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Did you declare Player before Monster?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. Problam with a global class variable
    By WebmasterMattD in forum C++ Programming
    Replies: 11
    Last Post: 01-21-2003, 04:38 AM
  5. Replies: 5
    Last Post: 06-01-2002, 11:24 PM