Perplexing error in member function.

Here is the member function:

You don't have to read all of it. It only concerns line #29 (the outermost numbers) - "moves_till_breed = 3;"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 22 void Ant::breed(vector<Ant> &ants)                                                                                
 23 {                                                                                                                 
 24     vector<Tuple> the_neighbors = neighbors();                                                                    
 25                                                                                                                   
 26     vector<Tuple> valid_choices;                                                                                  
 27     Tuple valid_choice;                                                                                           
 28     bool valid;                                                                                                   
 29     for (unsigned int i = 0; i < the_neighbors.size(); i++)                                                       
 30     {                                                                                                             
 31         valid = true;                                                                                             
 32         if (grid[the_neighbors[i].y][the_neighbors[i].x] != '-')                                                  
 33             valid = false;                                                                                        
 34         if (valid)                                                                                                
 35         {                                                                                                         
 36             valid_choice.y = the_neighbors[i].y;                                                                  
 37             valid_choice.x = the_neighbors[i].x;                                                                  
 38             valid_choices.push_back(valid_choice);                                                                
 39         }                                                                                                         
 40     }                                                                                                             
 41                                                                                                                   
 42     if (valid_choices.size() > 0)                                                                                 
 43     {                                                                                                             
 44         Tuple offspring_coords;                                                                                   
 45         int randomizer = rand() % valid_choices.size(); //randomizes choice of next valid move.                   
 46         offspring_coords.y = valid_choices[randomizer].y;                                                         
 47         offspring_coords.x = valid_choices[randomizer].x;                                                         
 48                                                                                                                   
 49         ants.push_back(Ant(offspring_coords.x, offspring_coords.y));                                              
 50         moves_till_breed = 3;                                                                                     
 51         return;                                                                                                   
 52     }                                                                                                             
 53 }                                                    


Now here is the problem..

In my main file, I have:

1
2
3
4
 
70     ants[0].set_breed(10);
71     ants[0].breed(ants);
72     cout << ants[0].get_moves_till_breed();


The function "set_breed" sets "moves_till_breed".

line #72 outputs "10", but it should output "3". I worked on this for hours and I cannot find any solution. It seems very strange.

"ants" is a vector of Ant objects that I have already declared.

I forgot to add that I did trace the breed() member function and line #29 is in fact executed.
Last edited on
Possibly not providing enough code?

line 24, what are you trying to do. what does that function return?
neighbors() returns a vector of Tuple objects that I created. Tuple is just a struct with 2 int member variables x and y. The neighbors function returns the surrounding coordinates of the calling Ant object in a 20x20 grid of characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
84 vector<Tuple> Organism::neighbors()
 85 {
 86     vector<Tuple> neighbors;
 87 
 88     neighbors.push_back(Tuple(coords.x, coords.y-1));
 89     neighbors.push_back(Tuple(coords.x-1, coords.y));
 90     neighbors.push_back(Tuple(coords.x+1, coords.y));
 91     neighbors.push_back(Tuple(coords.x, coords.y+1));
 92 
 93     for (unsigned int i = 0; i < neighbors.size(); i++) //gets rid of out-of-bounds neighbors
 94         if ((neighbors[i].x > 19) || (neighbors[i].y > 19) || (neighbors[i].x < 0) || (neighbors[i].y < 0))
 95         {
 96             neighbors.erase(neighbors.begin() + i);
 97             i--;
 98         }
 99 
100     return neighbors;
101 }
I found out that if I move the line "moves_till_breed = 3" outside of the 'if' block, the member variable gets changed. This doesn't make any sense though because I traced my program using gdb and I am positve that valid_choices.size() is greater than 0, so the if block is executed.

I also noticed something else that seems wrong with my debugger. When tracing programs using "next" in gdb, gdb sometimes randomly re-traces lines over again from 10-20 lines before without there being any loops in the program. Could this be a problem with gdb or the g++ compiler?
Last edited on
Topic archived. No new replies allowed.