Thread: A function that takes and send a pointer

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    54

    A function that takes and send a pointer

    Ok, so this will probably look ugly to some. I am trying to send an item that belongs to a character to a function that takes that item and sends it to another function to use that item. The reason I used the redirection was because I didnt know how to do something such as:

    player->inventory[0].useItem(player, inventory[0])

    here is the code i am working with.

    Code:
    // item.h
    struct item
    {
    	char name[100];
    	int price;
    	int quantity;
    	int enumValue;
    	void useItem(character *inChar, item *inItem);
    };
    Code:
    // item.cpp
    void useItem(character *inChar, item *inItem)
    {
    	using namespace std;
    	switch (inItem->enumValue)
    	{
    	case eMinorHealthPotion:
    		{
    			cout << inChar->showName() << " uses a minor healing potion.\n";
    			int heal = 32;
    			if (inChar->showIsAlive())
    			{
    				if ((inChar->showCurrentHealth() + heal) > inChar->showMaxHealth())
    				{
    					heal = heal - (((inChar->showCurrentHealth() + heal)) - inChar->showMaxHealth());
    					inChar->changeHealth(true, heal);
    				}
    				else
    					inChar->changeHealth(true, heal);
    
    				cout << inChar->showName() << " has been healed for " << heal << " hit points!\n";
    			}
    			else
    				cout << "The minor healing potion has no effect on " << inChar->showName() << '\n';
    
    			inItem->quantity--;
    			break;
    		}
        }
    }
    Code:
    // character.cpp
    void character::useItem(character *inChar, item *inItem)
    {
    	if (inItem->quantity > 0)
    		inItem->useItem(inChar, inItem);
    }
    Code:
    // main.cpp
    player->useItem(player, player->inventory[eMinorHealthPotion]);

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I think that you are forgetting that the 'this' pointer is available inside any member function. So you don't need to keep passing around the character pointer. And really you should call it something other than "character"...it took me a second to realize that you weren't talking about a char*...maybe calling it "player" is better. Anyway neither here nor there. Not sure if the code is exactly correct, but hopefully you get the idea. So:

    In main:
    Code:
    player->useItem(eMinorHealthPotion);
    In character.cpp:
    Code:
    void character::useItem(int index)
    {
    	if (inventory[index]->quantity > 0) {
    	    switch (inventory[index]->enumValue) {
    	         case eMinorHealthPotion: 
                             cout << showName() << " uses a minor healing potion.\n";
                             int heal = 32;
                             if (showIsAlive()) {
                                 if ((showCurrentHealth() + heal) > showMaxHealth()) {
    					heal = heal - (((showCurrentHealth() + heal)) - showMaxHealth());
    					changeHealth(true, heal);
    				}
    				else
    					changeHealth(true, heal);
    
    				cout << showName() << " has been healed for " << heal << " hit points!\n";
    			}
    			else
    				cout << "The minor healing potion has no effect on " << showName() << '\n';
    
    			inventory[index]->quantity--;
    			break;
                }
    
        }
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I think that you are forgetting that the 'this' pointer is available inside any member function.
    I read another thread where you were doing the same thing: you were preceding everything with "character::". You really need to get a book a read about the basics of using classes.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Well, the reason I was doing that was because of a book I was looking at had the first example like that. I have only been programming for 3 weeks so I never really got the the point of learning classes so I am trying to jump ahead at the moment. Thanks for the help.
    Last edited by JeremyCAFE; 12-21-2005 at 09:00 AM.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    I was wondering however, how would I be able to use the useItem function outside of the character.cpp file. If I wanted to use it in items.cpp I need to send both the character and the item.
    Is it even possible to use a function that belongs to an item like i had it before?

    item.func();

    player->inventory[0].func(); how does something like that work?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I have only been programming for 3 weeks so
    Then there's no point in using classes. Concentrate on the basics first. At this point, you are so unfamiliar with the subject matter that your questions don't make any sense.
    Last edited by 7stud; 12-21-2005 at 11:28 AM.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Im not sure how the question dosn't make any sense.

    I have the item struct with a function assosciated with it.

    in item.h:
    Code:
    struct item
    {
         ...
         useItem();
    }
    I want to be able to use my main character, player (which is a pointer)
    and have him use an item that is in his inventory (vector)

    declarations:

    in main.cpp:
    character *player = new character;

    in character.h : std::vector<item> inventory;


    im wondering how I could access the function that belongs to the item structure from main. Which is why i tried the incorrect method of : player->inventory[eMinorHealthPotion].useItem();
    Last edited by JeremyCAFE; 12-21-2005 at 11:53 AM.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Quote Originally Posted by 7stud
    Then there's no point in using classes. Concentrate on the basics first. At this point, you are so unfamiliar with the subject matter that your questions don't make any sense.
    In reponse to this, I don't have that option. Fullsail's classes are month long class (witn some exceptions). I am trying to get the most use out of pointers/refrences/etc while still being able to get a jump start on next class after break.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Which is why i tried the incorrect method of : player->inventory[eMinorHealthPotion].useItem();
    Actually that should work as long as inventory is declared public in character.h.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Quote Originally Posted by swoopy
    >Which is why i tried the incorrect method of : player->inventory[eMinorHealthPotion].useItem();
    Actually that should work as long as inventory is declared public in character.h.
    ah, damn. cant believe i didnt notice that. Thanks a lot. Just wondering, as far as standards go, it is more common for functions to alter variables of class which are private or simply make them public? I have been working to make as much private as possible with functions to alter them.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is generally better design to make the member variables private and provide public interface methods to access or alter them if other classes need to.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    As Daved said, private is generally better. It looks like in this case, you might be able to do what IfYouSaySo suggested, and pass the inventory's index to useItem.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Quote Originally Posted by swoopy
    As Daved said, private is generally better. It looks like in this case, you might be able to do what IfYouSaySo suggested, and pass the inventory's index to useItem.
    Yes, i went with this method. Only problem I noticed with having it in the character.cpp is that the 50 or so items will really lengthen the code.

    I think my main confusion came with visual studio. When I used the member operator it didn't give me what options I had so I assumed it was incorrect.

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    I figured I would keep my questions in this topic to avoid spamming threads. Having a little trouble again with my item handling functions. I am trying to error check the input. here is the code:

    Code:
    bool showItems(character *inCharPC)
    {
    	inCharPC->showItems();
    	std::cout << "Enter 0 to exit items menu.\n";
    
    	int itemInput = -1;
    	if (!(std::cin >> itemInput)) // checks for erros in the input buffer
    	{
    		std::cin.clear();
    		clearcin(); // while (cin.get() != '\n');
    	}
    
    	if ((itemInput > 0) && ((itemInput - 1) < int(inCharPC->inventory.size())  && inCharPC->inventory[itemInput - 1].quantity > 0))
    	{
    		inCharPC->useItem(itemInput - 1);
    		clearcin();
    		return false; // Tells calling loop the player is no longer active
    	}
    	else if(itemInput == 0)
    	{
    		clearcin();
    		return true; // remains in the loop as player is still active
    	}
    	else
    	{
    		clearcin();
    		return true; // remains in the  loop as player is still active
    	}
    }
    the problem I am having is when the user enters a character the program will wait for a second newline to be entered. I cant seem to figure out how to error check with char going into an int.
    Last edited by JeremyCAFE; 12-23-2005 at 10:04 AM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's because clearcin() is called twice if the input fails. The first time it clears whatever is in the stream until the newline from the user input, the second time it waits because there is no newline in the stream. Maybe you shouldn't call clearcin() in your first if since it will be called later on, or you should return from your first if since a character was entered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. Defining a custom function
    By Nalif in forum C Programming
    Replies: 3
    Last Post: 10-01-2006, 12:40 PM
  3. Confusion with * and &
    By Ganoosh in forum C++ Programming
    Replies: 32
    Last Post: 06-23-2005, 10:16 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM