Basic scope issue

This is a discussion on Basic scope issue within the C++ Programming forums, part of the General Programming Boards category; I'm trying to access a variable in a class from another class: Code: class hero ( public: int xpos; void ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    26

    Basic scope issue

    I'm trying to access a variable in a class from another class:

    Code:
    class hero (
       public:
         int xpos;
         void Move();
    );
    
    class monster (
       public:
         int xpos;
    );
    
    void hero::Move() {
         if (xpos = Goblin.xpos) {/*collision code*/}
    };
    
    hero MyHero;
    monster Goblin;
    Even though xpos is defined as public the compiler says it's not defined in this scope. I'm not sure what the best way to go about this would be.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    First, double == in your if().

    Second, you might need the declaration of Goblin to come before your use.

    Finally, you might want to consider your class layout. Does xpos need to be public? Are goblins and heros not just two instances of the same ilk - perhaps have one class called "creature", and if goblins / heros need something special, make a descendant class monster/hero, but keep common characteristics in the base class creature (like position, health, etc.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Oh, I already had ==, I just got sloppy copying the code :P

    Hmm, maybe I should pass Goblin to the function instead, that would make the most sense I think.

    So if I make goblin and hero derived classes of a master creature class (with xpos inherited from creature) will I be able to access xpos from a function defined in hero?

    I probably don't need xpos to be public in that case, since it will probably only be accessed by Move(). Should I make it Private or Protected?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Hmm, maybe I should pass Goblin to the function instead, that would make the most sense I think.
    To decide on how the Move() function should work, and thus what arguments it should take, if any, you need to be clear as to what it does.

    So if I make goblin and hero derived classes of a master creature class (with xpos inherited from creature) will I be able to access xpos from a function defined in hero?
    Yes, if xpos is protected. If xpos is private, then you should provide a protected function to access it.

    I probably don't need xpos to be public in that case, since it will probably only be accessed by Move(). Should I make it Private or Protected?
    You may want to read I've been told to never use protected data, and instead to always use private data with protected access functions. Is that a good rule?

    Incidentally, it is more common to use capitalised names for classes, but you seem to have inconsistently used this naming convention for variables (xpos is not Xpos, but you have MyHero and Goblin). If you do not have a naming convention, you might want to adopt one in which class names are capitalised and variable names are not.
    Last edited by laserlight; 07-05-2008 at 10:23 AM.
    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

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Yeah, my convention is basically just personal aesthetics at the moment, I should probably make it more consistent.

    Heh, thanks for the link, I know exactly the sort of people it was talking about.

    The Move function is actually the collision detection/reaction code now, I should really rename it. So I think it makes sense to pass it a monster, so that it tests for collisions between the hero and the monster it gets passed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 08:32 PM
  3. Replies: 10
    Last Post: 11-22-2007, 11:13 PM
  4. Need a basic Linux web page designer (editor)
    By BobS0327 in forum Tech Board
    Replies: 5
    Last Post: 12-30-2006, 04:30 PM
  5. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM

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