Jump to content
  • Advertisement
Sign in to follow this  
rom7L

returning a pointer to a STL list object

This topic is 5412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

my program creates a graph from names in a text file by creating a linked list of nodes that have a (string) name. i have a function that serches through the nodes list to find out if the node is already in the list. the function is passed a string (called key), which is the name of the node it is looking for. if it finds the node, it returns a pointer to the node (node*). the search function uses an iterator to search through the nodes list (nList). however, i haven't been able to sucsessfully return a pointer to the node, i jsut get garbage. i think this is because the iterator is creating a copy of the node, which is only relative to the scope of the search function. when the address is returned to the caller, it is garbage. here's what it looks like:
//the node struct and nodeList class
struct node
{
	string name;
	list<node*> edgeList;
        node(string nodeName)
	   {
		   name = nodeName;
	   }
};

class nodeList 
{
private: 
	list<node> nList;

public:
        void graph();
	void printGraph();
	node* search(string);
};


//graph function - makes the call to search()
void nodeList::graph()
{   
	node* keyPtr = NULL;
	node* valuePtr = NULL;
   	string key, value;
        ifstream iFile("test.txt");
   
	//read file, get key and value
	while (!iFile.eof()) 
	   {   
	      iFile >> key >> value;
	      //find key in node list, set pointer to key, set pointer to edgeList
		  keyPtr = search(key);
		
		  //if key not found, add that node to the list 
                  if (keyPtr == NULL)
		     {
				 nList.push_back(node(key));
				 keyPtr = &nList.back();
		     }

		  //search node list for value, if 'value' not in list, add it
		 valuePtr = search(value);
                 if (valuePtr == NULL)
		    {
				nList.push_back(node(value));
				valuePtr = &nList.back();
	            }

		 keyPtr->edgeList.push_back(valuePtr);
	   }
}



//search function
node* nodeList::search(string key)
{   
	//int count=0;
	//node* temp = NULL;
	typedef list<node>::iterator ListIterator;
	for (ListIterator l = nList.begin(); l != nList.end(); l++) 
	   {
		   if ( stricmp( l->name.c_str(), key.c_str()) == 0 )
		      {

				 return (node*)&l;
			  }
			
	   }
   return NULL;
}
i'm thinking of reimplementing the node list by using map<> instead of list<>

Share this post


Link to post
Share on other sites
Advertisement
A lot of weird things your doing, why are you using stricmp when string has an overloaded equality operator? also you do relize STL containers & iterators return constant & non-constant references to the elements? also you don't need to write a search function you can use STL algorithms std::find_if with a predicate.

I know you've posted before about this, i don't know if you saw my post, i recommended you use STL multimap for what your trying to do.

Quote:
Original post by C-Junkie
change &l to l

the iterator IS the pointer, for all practical purposes.


The way he has it thats not going to work he needs to "deference" the iterator which returns a reference to the element, if he really needs to return a pointer he needs to take the address aswell e.g.


return &(*l);

Share this post


Link to post
Share on other sites
Don't do this. An iterator might be a pointer for all practical purposes, but it doesn't have to be only a pointer. There is no guarantee casting will work.

What does work is: return &(*it);

Share this post


Link to post
Share on other sites
@snk_kid: yeah, i saw your post, and that would work, but i wanted to use list because it doesnt do any sorting.

but yes, you and trap are right. using &(*l) does exactly what i want it to do. this rocks, thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!