Thread: Basic scope issue

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 09:32 PM
  3. Replies: 10
    Last Post: 11-23-2007, 12:13 AM
  4. Need a basic Linux web page designer (editor)
    By BobS0327 in forum Tech Board
    Replies: 5
    Last Post: 12-30-2006, 05:30 PM
  5. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM