Thread: Function in a private member of a class?

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Function in a private member of a class?

    I tried putting a function in a private member of a class and I am getting all kinds of errors. I don't think I am doing it the right way. Can someone please help. This is how I have it set up.
    Code:
    class Weapons {
          public:
                 Weapons();
                 char getWeaponValue(); //Legendary, Epic, unique, rare, magical, normal
                 char getWeaponType(); //Sword, mace, staff
                 char getWeaponName(); //Name the weapon
                 int getWeaponStats(); // damage, +mods and durability
                 int getWeaponLevel(); // level of the weapon
                 void whoCanDrop();  //check to see which mobs can drop it
          private:
                  string WeaponName;
                  string WeaponType;
                  string WeaponValue;
                  int WeaponStats;
                  int WeaponDamage;
                  int WeaponDurability;
                  int WeaponLevel;
                  char CanIDropIt;
                  };
    int Weapons::getWeaponStats()
    {
        int x; //random variable to create random numbers
        int y; //random variable to decide stat
        int z; //random variable to decide number of a single stat
        srand(time(0));
        
        if (WeaponType == "Sword" && WeaponValue == "Normal" && Clvl <= 3)
        {
           WeaponStats = 0;
           WeaponDamage = 1 + (rand() % 10) - Clvl; 
           WeaponDurability = 1 + rand() % 20;
        }
        if (WeaponType == "Sword" && WeaponValue == "Normal" && Clvl <= 5)
        {
           WeaponStats = 0;
           WeaponDamage =  Clvl + (rand() % 20); 
           WeaponDurability = 1 + rand() % 30;
        }
        if (WeaponType == "Sword" && WeaponValue == "Normal" && Clvl <= 10)
        {
           WeaponStats = 0;
           WeaponDamage =  Clvl + (rand() % 40); 
           WeaponDurability = 1 + rand() % 50;
        }
                     
            RandmStats();      
                          
           //WeaponStats = 0;
           WeaponDamage = 1 + (rand() % 10) - Clvl; 
           WeaponDurability = 1 + rand() % 20;
    }
         
    }
    };
    
    int RandmStats()
    {
    //------------------------------  Function I want to make starts here
        if (WeaponType == "Sword" && WeaponValue == "Magical" && Clvl <= 3)
        {
           if (WeaponName == "Sword of the bear") // +str(lots), +hp(lots) and small amount of +agi(small) 
           {
             x = 1 + (rand() % 5) - 2; // how many mods
             switch (x)
             {
                    case 1:
                         y = 1 + rand() % 6; // which mod it gets 1 = str 2 = hp 3 = agi 4 = str&hp 5 = hp&agi 6 = all
                         if (y == 1)
                         {
                               z = 1 + rand() % 4; // z = the amount of the specified mod
                               if (z == 1)
                               {
                                     WeaponMod1 = 3 + Clvl;
                               }
                               if (z == 2)
                               {
                                     WeaponMod1 = 5 + Clvl;
                               }
                               if (z == 3)
                               {
                                     WeaponMod1 = 7 + Clvl;
                               }
                         if (y == 2)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               {
                                     WeaponMod2 = 5 + Clvl;
                               }
                               if (z == 2)
                               {
                                     WeaponMod2 = 10 + Clvl;
                               }
                               if (z == 3)
                               { 
                                     WeaponMod3 = 15 + Clvl;
                               }
                         }
                         if (y == 3)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               WeaponMod4 = 2;
                               if (z == 2)
                               WeaponMod4 = 4;
                               if (z == 3)
                               WeaponMod4 = 6;
                         }
                         if (y == 4)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               {
                                     WeaponMod1 = 3 + Clvl;
                                     WeaponMod2 = 5 + Clvl;
                               }
                               if (z == 2)
                               {
                                     WeaponMod1 = 5 + Clvl;
                                     WeaponMod2 = 10 + Clvl;
                               }
                               if ( z == 3)
                               {
                                    WeaponMod1 = 7 + Clvl;
                                    WeaponMod2 = 15 + Clvl;
                               }
                         }
                         if (y == 5)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               {
                                     WeaponMod2 = 5 + Clvl;
                                     WeaponMod4 = 2;
                               }
                               if (z == 2)
                               {
                                     WeaponMod2 = 10 + Clvl;
                                     WeaponMod4 = 4;
                               }
                               if (z == 3)
                               {
                                     WeaponMod2 = 15 + Clvl;
                                     WeaponMod4 = 6;
                               }
                         }
                         if (y == 6)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               { 
                                     WeaponMod1 = 3 + Clvl;
                                     WeaponMod2 = 5 + Clvl;
                                     WeaponMod4 = 2;
                               }
                               if (z == 2)
                               {
                                     WeaponMod1 = 5 + Clvl;
                                     WeaponMod2 = 10 + Clvl;
                                     WeaponMod4 = 5;
                              }
                              if (z == 3) 
                              {
                                    WeaponMod1 = 7 + Clvl;
                                    WeaponMod2 = 15 + Clvl;
                                    WeaponMod4 = 7;
                              }
                         }     
           }
         }
    // ----------------End the function I want to make
    This is giving me so many errors....I hade to cut and past from my orginal code so throw out all the syntax errors and/or missing {}.

    EDITED: Posted the code on how I am trying to get it to work.
    Last edited by RealityFusion; 09-02-2005 at 11:17 AM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I think I've found your mistake. It looks like your missing a brace at the "y == 1" block.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I think you have to tell us more about the function you want to make.
    Should it become a memberfunction of Weapons ? If so there should not be a problem to access a private member.
    If that new function should be a member of another class inheriting from Weapons than you canot access private members. Maybe to make them protected would be right. Another way to go would be to give Weapons public accessor-functions.
    But I'm just guessing. Need more Iinfo.
    Kurt

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    Code:
    int Weapons::getWeaponStats()
    {
        int x; //random variable to create random numbers
        int y; //random variable to decide stat
        int z; //random variable to decide number of a single stat
        srand(time(0));
        
        if (WeaponType == "Sword" && WeaponValue == "Normal" && Clvl <= 3)
        {
           WeaponStats = 0;
           WeaponDamage = 1 + (rand() % 10) - Clvl; 
           WeaponDurability = 1 + rand() % 20;
        }
        if (WeaponType == "Sword" && WeaponValue == "Normal" && Clvl <= 5)
        {
           WeaponStats = 0;
           WeaponDamage =  Clvl + (rand() % 20); 
           WeaponDurability = 1 + rand() % 30;
        }
        if (WeaponType == "Sword" && WeaponValue == "Normal" && Clvl <= 10)
        {
           WeaponStats = 0;
           WeaponDamage =  Clvl + (rand() % 40); 
           WeaponDurability = 1 + rand() % 50;
        }
                     
            RandmStats(); //Function call I am having problems with       
                          
           //WeaponStats = 0;
           WeaponDamage = 1 + (rand() % 10) - Clvl; 
           WeaponDurability = 1 + rand() % 20;
    }
         
    }
    };
    
    int RandmStats()
    {
    //------------------------------  Function I want to make starts here
        if (WeaponType == "Sword" && WeaponValue == "Magical" && Clvl <= 3)
        {
           if (WeaponName == "Sword of the bear") // +str(lots), +hp(lots) and small amount of +agi(small) 
           {
             x = 1 + (rand() % 5) - 2; // how many mods
             switch (x)
             {
                    case 1:
                         y = 1 + rand() % 6; // which mod it gets 1 = str 2 = hp 3 = agi 4 = str&hp 5 = hp&agi 6 = all
                         if (y == 1)
                         {
                               z = 1 + rand() % 4; // z = the amount of the specified mod
                               if (z == 1)
                               {
                                     WeaponMod1 = 3 + Clvl;
                               }
                               if (z == 2)
                               {
                                     WeaponMod1 = 5 + Clvl;
                               }
                               if (z == 3)
                               {
                                     WeaponMod1 = 7 + Clvl;
                               }
                         if (y == 2)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               {
                                     WeaponMod2 = 5 + Clvl;
                               }
                               if (z == 2)
                               {
                                     WeaponMod2 = 10 + Clvl;
                               }
                               if (z == 3)
                               { 
                                     WeaponMod3 = 15 + Clvl;
                               }
                         }
                         if (y == 3)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               WeaponMod4 = 2;
                               if (z == 2)
                               WeaponMod4 = 4;
                               if (z == 3)
                               WeaponMod4 = 6;
                         }
                         if (y == 4)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               {
                                     WeaponMod1 = 3 + Clvl;
                                     WeaponMod2 = 5 + Clvl;
                               }
                               if (z == 2)
                               {
                                     WeaponMod1 = 5 + Clvl;
                                     WeaponMod2 = 10 + Clvl;
                               }
                               if ( z == 3)
                               {
                                    WeaponMod1 = 7 + Clvl;
                                    WeaponMod2 = 15 + Clvl;
                               }
                         }
                         if (y == 5)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               {
                                     WeaponMod2 = 5 + Clvl;
                                     WeaponMod4 = 2;
                               }
                               if (z == 2)
                               {
                                     WeaponMod2 = 10 + Clvl;
                                     WeaponMod4 = 4;
                               }
                               if (z == 3)
                               {
                                     WeaponMod2 = 15 + Clvl;
                                     WeaponMod4 = 6;
                               }
                         }
                         if (y == 6)
                         {
                               z = 1 + rand() % 4;
                               if (z == 1)
                               { 
                                     WeaponMod1 = 3 + Clvl;
                                     WeaponMod2 = 5 + Clvl;
                                     WeaponMod4 = 2;
                               }
                               if (z == 2)
                               {
                                     WeaponMod1 = 5 + Clvl;
                                     WeaponMod2 = 10 + Clvl;
                                     WeaponMod4 = 5;
                              }
                              if (z == 3) 
                              {
                                    WeaponMod1 = 7 + Clvl;
                                    WeaponMod2 = 15 + Clvl;
                                    WeaponMod4 = 7;
                              }
                         }     
           }
         }
    // ----------------End the function I want to make
    I edited the code above to look like this. By doing this everything that is in bold makes an error, which makes sence because I am trying to access data from a private member in the RandmStats() function.
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    int Weapons::RandmStats(){
    should solve your problems.
    kurt

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by ZuK
    Code:
    int Weapons::RandmStats(){
    should solve your problems.
    That and declaring the function as a member of your class:
    Code:
    class Weapons {
    public:
    // public stuff ...
    private:
      int RandmStats();
      // other private stuff...
    };

  7. #7
    1479
    Join Date
    Aug 2003
    Posts
    253
    ..... I think I have just been working on this for to long. Thanks, it seems so simple now.
    Knowledge is power and I want it all

    -0RealityFusion0-

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    You've got some errors to fix. Local variables such as x and y aren't defined. Your braces don't all match. And your using variables such as x, y, WeaponMod1, etc that aren't defined in the private method.

  9. #9
    1479
    Join Date
    Aug 2003
    Posts
    253
    Yeah, the snippets I provided were cut and paste like I said so a few brackets and declarations were left out. But while you said that, the WeaponModX variables were declared globaly outside of the class....is that going to be a problem later on in my program?
    Knowledge is power and I want it all

    -0RealityFusion0-

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    the WeaponModX variables were declared globaly outside of the class....is that going to be a problem later on in my program?
    Hard to tell from the source, but you should probably best kept in an array or part of another global object. Managing them, I think, could be difficult if you have many functions chaning the WeaponModelX. But if it's just that one function modifying you might not have too much trouble if you can ensure that the function is correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Is it possible to have callback function as a class member?
    By Aidman in forum Windows Programming
    Replies: 11
    Last Post: 08-01-2003, 11:45 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM