Thread: problem with classes in a list

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    19

    Exclamation problem with classes in a list

    Hi I'm new to this forum so if anything I do is a problem I'm quite sorry.

    I am programming a game at the moment to consolidate my c++ skills, improve and have something to extend when I decide to learn graphical programming. However I have come across a brick wall and although i could come up with a work-around I would like to solve my problem rather than run away from it.

    On to the problem!
    I'm trying to create a function that can check if any sprite classes in a list are located at the position passed into the function and then return the address of the class so that it can be edited.

    If I have found a 'sprite' which has the position desired I will return true and return the address of the 'sprite' class by using the pointer.

    My problem is assigning the address of the 'sprite' class to the pointer sent in the parameters.

    Code:
    bool sprite::checkSpritePos(std::list<sprite*> spriteList, int xpos, int ypos, sprite *spriteAddress)
    {
        std::list<sprite *>::iterator Iter;
    
        for (Iter = spriteList.begin(); Iter != spriteList.end(); Iter++)
        {
            if ((int)((*Iter)->getX()) == xpos && (int)((*Iter)->getY()) == ypos)
            {
                // need to copy the address of the desired class to a pointer
                return true;
            }
        }
        return false;
    }
    I'm am not sure what code needs to go there. My ideas have just caused compile errors.

    Any help would be very appreciated and for any time spent trying to help me.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The caller isn't seeing any changes you make, eh?

    In the argument list, declare the fourth argument as sprite **spriteAddress (note the two asterixes).

    In place of your comment, add the line "*spriteAddress = *Iter;"

    Better yet, remove that argument completely, change the return type from bool to sprite *, replace "return true" with "return *Iter", and replace "return false" with "return 0"
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Thank you, it was so easy to overlook the two asterixes.

    I have constructed this function this way as it is an overloaded function depending on if I want to return the sprite or not.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Hi again, this function is causing me errors at runtime now.

    Code:
     bool sprite::checkSpritePos(std::list<sprite*> spriteList, int xpos, int ypos, sprite **spriteAddress)
    {
        std::list<sprite *>::iterator Iter;
        spriteAddress = 0;
    
        for (Iter = spriteList.begin(); Iter != spriteList.end(); Iter++)
        {
            if ((int)((*Iter)->getX()) == xpos && (int)((*Iter)->getY()) == ypos)
            {
                *spriteAddress = *Iter; // error occurs here
                return true;
            }
        }
        return false;
    }

    The line *spriteAddress = *Iter; is causing an unhandled exception.

    Here is the message that shows up when I run the program in degugging mode.

    Unhandled exception at 0x0041f4a0 in snake game.exe: 0xC0000005: Access violation writing location 0x00000000.

    All these fancy numbers are making that message very cryptic for me right now. Thanks for any help.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The problem will be in how you call the function. You are passing a NULL pointer (address zero) to spriteAddress.

    Whenever you do
    Code:
        *anything = something;
    where anything is a pointer (in your case, a pointer to a pointer) then anything cannot be a NULL pointer.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    I am not sure what to do. What can I do to initialize anything without using a NULL pointer?
    Should I just create a new sprite class pointer for the spriteAddress

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Basically all you need to do, when calling your function is
    Code:
        sprite  *the_address = NULL;
        bool retval = checkSpritePos(your_list, x, y, &the_address);
    
         //   your_list, and all the pointers in it, need to be valid here.   If not, the_address will not be valid
    Note the ampersand on the_address when calling your function. the_address may start life as a NULL pointer. But &the_address is not a NULL pointer.

    My guess is that what you're doing wrong is something like this.
    Code:
        sprite  **the_address = NULL;
        bool retval = checkSpritePos(your_list, x, y, the_address);     // boom, crash.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Thanks for that, figuring out that would have taken me quite a while to nut out myself.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    If you are going to use grumpy's code you must remove line 4 from tha function
    Code:
    spriteAddress = 0;
    Local spriteAddress is set to point to the_address but if you set it to 0 you'll get the same problem as before: dereferencing pointer that doesn't exist.

    Also, you should declare your function as
    Code:
    bool sprite::checkSpritePos(const std::list<sprite*>& spriteList, int xpos, int ypos, sprite **spriteAddress)
    to avoid creating local list upon every call.
    In that case, local Iter must be const iterator:
    Code:
    std::list<sprite *>::const_iterator Iter;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. list of classes
    By vril in forum C++ Programming
    Replies: 5
    Last Post: 11-21-2010, 04:18 PM
  2. Constructor problem in list of classes
    By G4B3 in forum C++ Programming
    Replies: 17
    Last Post: 06-01-2009, 03:13 AM
  3. STL List container with classes
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2005, 02:13 PM
  4. write/read list of classes to file
    By nijmegen in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2002, 09:23 AM
  5. List of derived classes
    By Asmodan in forum C++ Programming
    Replies: 1
    Last Post: 05-28-2002, 08:46 PM