Iterator not accessing values properly

This is a discussion on Iterator not accessing values properly within the C++ Programming forums, part of the General Programming Boards category; Hmm.. in my code I iterate through a list(an actual list data structure) and I run a member function of ...

  1. #1
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532

    Iterator not accessing values properly

    Hmm.. in my code I iterate through a list(an actual list data structure) and I run a member function of each instance of my Node class that the iterator is pointing to. Problem is, when I run the member function(which is supposed to return a stored value in the Node class) it ALWAYS returns 0. When I was debugging, I found this, and I printed the return value of the member function of the node that the iterator was pointing to(supposed to be the same value that was returned when running the same member function on the iterator) and it gave me the correct value. So this throws off my whole algorithm. The problem function is below:
    Code:
    Coord find_lowest_fval()
    {
    	int lowest=999;
    	int temp;
    	Coord winner;
    	for(iter=openlist.begin();iter!=openlist.end();iter++)
    	{
    		temp=iter->get_fval();
    		if(temp<lowest)
    		{
    			lowest=temp;
    			winner=iter->get_pos();
    		}
    	}
    	return winner;
    }
    Coord is a struct containing two int's(x and y). The openlist is my list, and that's about it. I'm wondering why the "iter->get_fval()" function is ALWAYS returning 0 when it should be returning the correct value? Am I accessing it properly?? Any help or insight is appreciated, thanks.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Well I did make up a workaround, but still am curious as to why my original code didn't work. With my workaround I got the coordinates of the node and identified it that way to obtain it's f value. So that part works now.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    can you show the lines declaring iter and openlist, also the function get_fval

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Is the return value an integer? You declare temp as an integer. If you are trying to return a fractional value that is less then 1 you would need to declare it as a double or float. I am not sure what f value is but when I googled for it it seems to be a relationship where one value is divided by another.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yes, the f value is an integer, and no it's not a fractional value, it's a whole number. I set the f values based on location and internal node values, so I'm positive they're integers. The f I used to name my value is used in many places so google wouldn't help. However, in my program, I'm using f as a sum of two numbers. The sum of the "movethrough" value(the value each node has(unique)) and the heuristic value(Manhattan method).
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Just as a design pointer -

    int lowest=999;

    while it's functional, I'd suggest that you assign it to perhaps the first element of openlist against the possibility that all the numbers are above 999. Makes it more fool proof and it's pretty much just as easy to program.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Why aren't you using the STL min_element algorithm with an appropriate binary predicate?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Just as a design pointer -

    int lowest=999;

    while it's functional, I'd suggest that you assign it to perhaps the first element of openlist against the possibility that all the numbers are above 999. Makes it more fool proof and it's pretty much just as easy to program.
    Or, on the off chance that there are no elements in openlist(), INT_MAX from <climits>. Unless you plan to handle that case separately.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    brewbuck, not too familiar with that, but I don't think I can because all the items in openlist are of class Node. What values I'm examining are private member integer variables that are returned with the "get_fval()" member function.
    Well my openlist and iter are declared as such:
    Code:
    std::list<Node> openlist;
    std::list<Node> closelist;
    std::list<Node>::iterator iter;
    Those are global variables.
    And my Node class looks like this:
    Code:
    class Node
    {
    	private:
    		Coord position;
    		int mvthru;
    		int fval;
    		bool start;
    		bool end;
    		Coord parent;
    	public:
    		Node(int,int);
    		Node();
    		~Node();
    		
    		Coord get_pos(){return position;};
    		void set_pos(int x,int y){position.x=x;position.y=y;};
    		void set_movethrough(int val){mvthru=val;};
    		int get_movethrough(){return mvthru;};
    		void set_start(){start=true;};
    		void set_end(){end=true;};
    		bool is_start(){return start;};
    		bool is_end(){return end;};
    		void set_parent(Coord pos){parent.x=pos.x;parent.y=pos.y;};
    		Coord get_parent(){return parent;};
    		void set_fval(int f){fval=f;};
    		int get_fval(){return fval;};
    };
    Constructors/destructor omitted due to relevancy.
    Yeah the lowest variable's initialization was just done for speed, I'll go back and make it nicer/faster but I'm just trying to get it up and running as of now. Thanks for all the replies.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can pass a predicate to min_element that will tell it how to to test values. Just a simple bool function taking two const Node& parameters and returning true if the first's get_fval() is less than (but not equal to) the second's. You can also overload the operator< for the Node class to compare the get_fval() value, although that might not make sense for the full class.

    http://www.sgi.com/tech/stl/min_element.html

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Such a predicate is in fact very easy to write using Boost.Bind:
    Code:
    min_element(..., boost::bind(&Node::get_fval, _1) < boost::bind(&Node::get_fval, _2));
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IRC-type logger with files based on three values
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 08-27-2008, 05:42 PM
  2. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 02:37 PM
  3. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 06:29 PM
  4. need some advice on displaying values
    By rEtard in forum Windows Programming
    Replies: 7
    Last Post: 06-16-2005, 10:55 AM
  5. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 04:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21