Thread: Calling an object in a function

  1. #1
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128

    Question Calling an object in a function

    I have had this problem which i keep coming back to, and i have to solve it somehow now

    My Main function:
    Code:
    void main(){
    Character Luke;
    Luke.initialise;
    
    battle();
    }
    My class:
    Code:
    class Character
    {
     public:
        void initialise();
        int get_hp();
     private:
        int hp;
    My function:
    Code:
    void battle(){
      Luke.get_hp();
    };
    however i get the nice problem that "Luke is undeclared in this function" (the 'battle' function).
    I of course have several other functions which need to access "Luke.get_hp();" and other modules that i have in the class. So how can i get the "battle" function to be able to see that i have already created the object "Luke" and allow access to it?

  2. #2
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    btw, i think i need to somehow make the classes have a global scope. but i really cant think of a way to do that...

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Why don't you just pass the object into the function? Try this for the new prototype.

    Code:
    void Battle( const Character &temp )
    {
         temp.get_hp( );
    }
    Also in your main you call initialize without paranthesis. You need these because it is a function, remember.

  4. #4
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    thanks. only thing is now i get this error:

    passing `const Character' as `this' argument of `int Character::get_hp()' discards qualifiers


    now what does "discards qualifiers" mean? ive looked around, and havent found anything.

    oh and yeah, i know i was missing the (), i just forgot to put them in when i typed it.
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Remove the const keyword...

    It's better to pass the address of the Character object to the function:
    Code:
    void battle(Character *C)
    {
      C->get_hp();
    }
    
    In main:
    
    battle(&Luke);

  6. #6
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    if you're gonna pass the Character to different functions, i would declare Luke on the heap

    Code:
    int main()
    {
      Character* Luke = new Character;
      Luke->initialise();     // is initialize spelled wrong? lol
    
      battle(Luke);  // Luke is a pointer, passing the address
      return 0;
    }
    and use what monter had for the battle() function.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  7. #7
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    is initialize spelled wrong?
    no, here in aus, we use "s" not "z"

    and thanks for the help guys, i understand how to do it now
    i just gotta put it together
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM