Quick Question on Classes

This is a discussion on Quick Question on Classes within the C++ Programming forums, part of the General Programming Boards category; I don't seem to be able to get classes very well. For example, let's say I put.... Code: class Player ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Quick Question on Classes

    I don't seem to be able to get classes very well. For example, let's say I put....

    Code:
    class Player {
    int strength;
    }
    Player user;

    so now, how do I edit the value of user.strength. When I use

    Code:
    user.strength = 10;
    it gives error. Saying that there is a syntax error before the "." Sorry for noob question, but I'm new
    Last edited by gguy85; 02-14-2005 at 12:00 AM.

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Class members are private by default. If you wish to manipulate data members from outside class member functions, declare them as public...ie...

    Code:
    class Player {
        public:
            int strength;
    };
    Oh, and as a side note, you're missing a ; at the end of your class definition.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    yeah......that wasn't the actual code though. I do it public and with the ; at the end but it doesn't work :-/

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    Here's a quick look:
    Code:
           class Player {
          public:
      int health;
      int strength;
      int agility;
    
      void move();
      void attackMonster();
      void getTreasure();
    };
    Player user;
    user.strength = 10;
    gives error :-/

  5. #5
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Is that an actual cut/paste of the code as it appears in the program? Or related pieces shown together for convenience? Just curious since the compiler is giving you a syntax error, very well may be a simple typo (or something left out) somewhere in the few lines before the instance of user.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Generally you should not have public data members in a class; rather, you should use a struct or, in most cases, just make them private. Then you can have your member functions do all of the modifying of data.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In the future, try actually posting the errors you say you're getting.
    Code:
    class Player {
        public:
            int health;
            int strength;
            int agility;
    
            // void move();
            // void attackMonster();
            // void getTreasure();
    };
    
    int main()
    {
        Player user;
        user.strength = 10;
    
        return 0;
    }
    Compile. Run. Be amazed.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  2. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM
  3. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 02:54 AM
  4. * quick question *
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 03-27-2002, 06:58 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

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