Author Topic: Returning the memory location of a value held in an STL list.  (Read 4435 times)

0 Members and 2 Guests are viewing this topic.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
I'm working on exploring some new pathfinding code and currently have a problem.

I have a stl list of Tiles. I have written a search function that should search the stl list and return a pointer to the memory location of the Tile.
However, it appears that what I'm doing will not work because it wants to use a pointer to an iterator rather than a pointer to a Tile.


The understandable error:
Code: [Select]
/home/ryan/Dropbox/Rpath/main.cpp|110|error: cannot convert ‘std::_List_iterator<Tile>*’ to ‘Tile*’ in return|
Code: [Select]
struct Tile
{
public:
    int x,y,value,g,h,f; // a non zero value means unwalkable
    Tile * parent;

    Tile(Tile * parent, int x, int y, int g, int h, int value)
    {
        this -> parent = parent;
        this -> x = x;
        this -> g = g;
        this -> h = h;
        this -> value = value;
        this -> f = g + h;
    }

    bool operator == (Tile& compare)
    {
        // Tiles are only compared on x and y values
        // other variables are not taken into account
        if(compare.x != x) return false;
        if(compare.y != y) return false;
        return true;
    }

    bool operator < (const Tile& compare)
    {
        if(f < compare.f) return true;
    }
};

//  now the function that cannot compare an stl list iterator pointer to a Tile.

Tile* find_by_value(list<Tile>& my_list, Tile &my_tile)
{
    // return true if a similar tile is found
    for(list<Tile>::iterator i = my_list.begin(); i != my_list.end(); i++)
    {
        if(*i == my_tile) return &i;
    }
    return NULL;
}


The function is supposed to take a Tile and search the stl list to find a matching one, returning a pointer to the matching Tile.[/code]
« Last Edit: August 09, 2011 by Pixel_Outlaw »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Use "*" to get the actual object from the iterator and "&" to get the object's address:
Code: [Select]
list<Tile>::iterator it;
Tile *tile= &(*it);
Make sure your container returns a reference, otherwise your object will be copied and you're returning a pointer into the stack-frame which will be invalid after leaving the function.
« Last Edit: August 09, 2011 by hellfire »
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile

Ok thanks for the information.

Seems like pointers can get a person into a lot of trouble when trying to get fancy.
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
With std::vector, you can do:

Code: [Select]
std::vector v(10,42);
&v[0]

For std::list, I am not sure on how to do, and if it is really appropriate to do it (it could be linked list?)
The demoscene will never die, never!