Thread: This Has Been Troubling Me For Some Time.

  1. #1
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37

    Angry This Has Been Troubling Me For Some Time.

    Here is a header file in which I declare and define the class "Sebba": (My character)

    class Sebba
    {
    public:
    int GetHealth(int x);
    private:
    int Health;
    };
    int Sebba::GetHealth(int x)
    {
    return Health;
    }

    And here is the start of a C++ Source Code that I'm using in the game:

    1.#include <iostream.h>
    2.#include "header.h"
    3.main()
    4.{
    5.Sebba obj;
    6.cout << obj.GetHealth(5);
    7.return 0;
    8.}

    As you can see, an intenger value is passed through the GetHealth() funcion. What's giving me troubles is this:

    On line 6 of my source code, I want obj.Health's value to be printed to the screen. The GetHealth() Function is supposed to have the value 5 passed into it, return it, and print it to the screen. I expected it to print "5" to the screen. Much to my surprise, it printed out "4370436". I tried fixing this by changing intenger types, but after a lot of trouble and headaches, I still can't possibly imagine why it's printing out this number instead of 5. Can someone please help me figure out what's going on here?
    -Dean

  2. #2
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    health is uninitialized, so it has a random value (i think...). if you want to return whatever value is passed to getHealth, make it return 'x' (the value you passed to it). if you want it to return health, you need to initialize health (either in a constructor, or a setHealth() function).

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's correct. Try using any non-static local variable without initializing it and see what you get.

    On a design note, usually you want your functions something like:
    Code:
    int getHealth( void )
    {
        return health;
    }
    void setHealth( int x )
    {
        health = x;
    }
    Typicly speaking, 'get' functions take no arguments, and set functions return nothing.

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

  4. #4
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37

    :(

    Hmm....

    It still doesn't work
    -Dean

  5. #5
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    do you have some updated code you could post?

  6. #6
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37
    Well yeah,

    but it's pretty much the same exact thing as the code in my first post.
    -Dean

  7. #7
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    well what did you change? are you getting compile errors? is the number still coming out wrong?
    try this...

    Code:
    class Sebba
    {
    public:
       int GetHealth();
       int setHealth(int);
    private:
       int Health;
    };
    
    int Sebba::setHealth(int newHealth)
    {
       health = newHealth;
    }
    
    int Sebba::GetHealth()
    {
       return Health;
    }
    then in the main program...
    Code:
    #include <iostream>
    using std::cout;
    #include "header.h"
    
    main()
    {
       Sebba obj;
       obj.setHealth(5);
       cout << obj.GetHealth();
       return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by DeanDemon
    Well yeah,

    but it's pretty much the same exact thing as the code in my first post.
    Using Quzah's functions, it should look like this..

    Code:
    #include <iostream> // Input/Ouput
    #include <Header.h> // your header..
    using namespace std;
    
    int main( int argc, char *argv[] )
    {
      // Instantiate object
      Sebba obj; 
    
      // Set the health 
      obj.SetHealth( 123 ); 
    
      // Output health
      cout << obj.GetHealth( ) << endl;
    
      return 0;
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  9. #9
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37
    I got it fixed. Yay!
    -Dean

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM