Misconception about classes?

This is a discussion on Misconception about classes? within the C++ Programming forums, part of the General Programming Boards category; I was under the impression that all the functions in a class shared the variables that were defined in the ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40

    Misconception about classes?

    I was under the impression that all the functions in a class shared the variables that were defined in the class. Was I mistaken? My compiler keeps telling me that the variables in the class haven't been declared in the functions within the class that I use them in. So I guess I was wrong. If I am wrong, is there an easy way to solve this?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Could you post some minimal code that reproduces the issue, please?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40
    Code:
    class creature {
    	public:
    	creature () {}
    	~creature () {}
    	int attack () {
    		power = basic_dmg + mod_dmg;
    		return power;
    	}
    	void takedmg ( int dmg ) {
    		dmg = dmg - armor;
    		health = health - dmg;
    	}
    	private:
    	int basic_dmg, mod_dmg, power, dmg, armor, health;
    };
    error: 'power' undeclared. first use this function.
    error: 'basic_dmg' undeclared. first use this function.

    and so on for all the other variables, except for the ones within the arguments
    Last edited by cpudaman; 01-01-2008 at 08:52 PM. Reason: I forgot to post the error message.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    class creature {
        private:
        int basic_dmg, mod_dmg, power, dmg, armor, health;
        public:
        creature () {}
        ~creature () {}
        int attack () {
            power = basic_dmg + mod_dmg;
            return power;
        }
        void takedmg ( int dmg ) {
            dmg = dmg - armor;
            health = health - dmg;
        }
    };

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    That's rather strange. Your code looks correct, and compiles without errors on the MinGW port of g++ 3.4.5, MSVC8, and the online Comeau compiler. There should be no need to change it to robwhit's example. What compiler are you using?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40
    for writing purposes, i use codeblocks.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40
    my example compiles fine...hmmm, I thought I included the problem, here is the full class
    Code:
    class player {
        public:
            player (int health, int intelligence, int strength, int agility,
                    int stamina, int armormin, int armormax, string weap1name,
                    string weap1descr, string weap1shrtdescr, int weap1dmg,
                    int weap1moddmg, int weap1modtype, string weap2name,
                    string weap2descr, string weap2shrtdescr, int weap2dmg,
                    int weap2moddmg, int weap2modtype, string weap3name,
                    string weap3descr, string weap3shrtdescr, int weap3dmg,
                    int weap3moddmg, int weap3modtype ) {
            }
            ~player () {}
    
            int getagility ( void ) {
                return agility;
            }
    
            int attack ( void ) {
                cout << "Select you weapon of destruction\n\n";
                cout << "1. " << weap1name << "\n";
                cout << "2. " << weap2name << "\n";
                cout << "3. " << weap3name << "\n";
                cin >> choice;
                cin.ignore();
                switch ( choice ) {
                    case 1:
                    weap1dmg = dmg;
                    break;
                    case 2:
                    weap2dmg = dmg;
                    break;
                    case 3:
                    weap3dmg = dmg;
                    break;
                    default:
                    cout << "Don't be an idiot\n" << "You need to make a choice\nNow no attack for you!\n";
                    cin.get();
                    clrscrn();
                    break;
                }
                return dmg;
    
            }
            void takedmg( int dmg, int enemyagility) {
                if (agility > enemyagility) {
                    agilitydif = agility - enemyagility;
                }
                if (agilitydif < 10) {
                    dmg = (dmg * ( 1 - ( agilitydif / 10 ) ) );
                    def = rand() &#37; (armormax - armormin + 1) + armormin;
                    dmg = dmg - def;
                    if (dmg < 0) {
                        dmg = 0;
                    }
                    if (dmg != 0) {
                        cout << "Your armor blocked " << def << "DMG.\n";
                        cout << "\nYou took " << dmg << " DMG.\n:";
                        health = health - dmg;
                        cout << "\nYour health is down to " << health << " HP.\n";
                        cin.get();
                        clrscrn();
                    }
                    if (dmg == 0) {
                        cout << "No Damage Taken\n\n";
                        cout << "HP remains at " << health;
                        cin.get();
                        clrscrn();
                    }
                }
                if ( enemyagility <= agility ) {
                    cout << "You dodged the attack";
                    cin.get();
                    clrscrn();
                }
            }
    
        private:
            int health, intelligence, strength, agility, stamina, armormin, armormax;
            int choice, enemyagility, agilitydif, dmg, def;
    
    };

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    In your example, things such as weap1name are only declared as parameters in your class constructor, not as actual instance variables. You'll need to add declarations for them within the class itself.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    for writing purposes, i use codeblocks.
    Good for you, but I asked about compiler, not IDE. CodeBlocks might use g++, or perhaps a Microsoft compiler, or yet other compilers.

    my example compiles fine...hmmm, I thought I included the problem, here is the full class
    It is a good idea to test your examples before posting them
    Anyway, I believe XSquared got it right.

    That said, you may want to improve your class design a little. Trying to store everything about the player directly in the player class can be a little cumbersome. It may make sense to create other classes, e.g., armor and weapon classes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by cpudaman View Post
    my example compiles fine...hmmm, I thought I included the problem, here is the full class
    Code:
    class player {
        public:
            player (int health, int intelligence, int strength, int agility,
                    int stamina, int armormin, int armormax, string weap1name,
                    string weap1descr, string weap1shrtdescr, int weap1dmg,
                    int weap1moddmg, int weap1modtype, string weap2name,
                    string weap2descr, string weap2shrtdescr, int weap2dmg,
                    int weap2moddmg, int weap2modtype, string weap3name,
                    string weap3descr, string weap3shrtdescr, int weap3dmg,
                    int weap3moddmg, int weap3modtype ) {
            }
            ~player () {}
    
            int getagility ( void ) {
                return agility;
            }
    
            int attack ( void ) {
                cout << "Select you weapon of destruction\n\n";
                cout << "1. " << weap1name << "\n";
                cout << "2. " << weap2name << "\n";
                cout << "3. " << weap3name << "\n";
                cin >> choice;
                cin.ignore();
                switch ( choice ) {
                    case 1:
                    weap1dmg = dmg;
                    break;
                    case 2:
                    weap2dmg = dmg;
                    break;
                    case 3:
                    weap3dmg = dmg;
                    break;
                    default:
                    cout << "Don't be an idiot\n" << "You need to make a choice\nNow no attack for you!\n";
                    cin.get();
                    clrscrn();
                    break;
                }
                return dmg;
    
            }
            void takedmg( int dmg, int enemyagility) {
                if (agility > enemyagility) {
                    agilitydif = agility - enemyagility;
                }
                if (agilitydif < 10) {
                    dmg = (dmg * ( 1 - ( agilitydif / 10 ) ) );
                    def = rand() % (armormax - armormin + 1) + armormin;
                    dmg = dmg - def;
                    if (dmg < 0) {
                        dmg = 0;
                    }
                    if (dmg != 0) {
                        cout << "Your armor blocked " << def << "DMG.\n";
                        cout << "\nYou took " << dmg << " DMG.\n:";
                        health = health - dmg;
                        cout << "\nYour health is down to " << health << " HP.\n";
                        cin.get();
                        clrscrn();
                    }
                    if (dmg == 0) {
                        cout << "No Damage Taken\n\n";
                        cout << "HP remains at " << health;
                        cin.get();
                        clrscrn();
                    }
                }
                if ( enemyagility <= agility ) {
                    cout << "You dodged the attack";
                    cin.get();
                    clrscrn();
                }
            }
    
        private:
            int health, intelligence, strength, agility, stamina, armormin, armormax;
            int choice, enemyagility, agilitydif, dmg, def;
    
    };
    I see a bunch of undeclared variables.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40
    Thanks for all the help, I got the problem fixed and understand what I did wrong.

    Quote Originally Posted by laserlight View Post
    ]
    That said, you may want to improve your class design a little. Trying to store everything about the player directly in the player class can be a little cumbersome. It may make sense to create other classes, e.g., armor and weapon classes.
    I'll try to improve the organization, but I wanted there to be a separate function for the fighting, than there was for the scenario stuff.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    All those variables with 1,2,3 in them are screaming to be arrays, or better yet, std::vector's
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    They also scream of being put into a single weapon struct.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21