2014-02-22 98 views
0

編輯:該問題已通過迭代索引並將向量作爲參考傳遞給函數來解決。向量迭代器沒有循環遍歷整個向量

我正在創建一個元胞自動機般的程序。有兩類 - 螞蟻和doodlebugs。螞蟻用char'O'表示,而doodlebug用char'X'表示。 2D char數組上的空格是'。'

我有一個動態矢量來保存指向對象的指針,隨着對象數量的增加和減少隨着時間的推移。

我目前正在測試我的Ant類。經過三個移動步驟後,螞蟻會繁殖。這兩個函數可以工作,所以我正在測試最後一個Ant函數 - 死亡。

我正在測試殺死一個螞蟻的初始函數(只需將它從一個字母翻到數組中的'。'中),但它似乎並沒有遍歷整個螞蟻向量。如果我沒有螞蟻的品種,它會按預期工作。「

例如,我從6個螞蟻開始。我讓他們四處走動,然後把他們都殺了。有用。

我從6只螞蟻開始,讓它們移動3次,繁殖,然後嘗試全部殺死它們。只有一些對象「死亡」(實際上轉向'。')

我假設問題是繁殖功能 - 是關於如何添加干擾迭代的新對象?

這裏是代碼的相關位:

class Ant : public Organism { 
    Ant(char array[][20]) { 
      this->x = rand() % 20; 
      this->y = rand() % 20; 
      array[x][y] = 'O'; 
      this->count = 0; 
    } 
    Ant(char array[][20], int itsX, int itsY) { 
      this->x = itsX; 
      this->y = itsY; 
      array[x][y] = 'O'; 
      this->count = 0; 
    } 

    // If adjacent cell = '.', push back a new Ant object 
    void breed(char array[][20], std::vector<Ant*> colony) { 
      if (this->count == 2) { 
        if(!occupied_down(array)) { 
          Ant* temp = new Ant(array, x+1, y); 
          colony.push_back(temp); 
        } else if(!occupied_up(array)) { 
          Ant* temp = new Ant(array, x-1, y); 
          colony.push_back(temp); 
        } else if(!occupied_right(array)) { 
          Ant* temp = new Ant(array, x, y+1); 
          colony.push_back(temp); 
        } else if(!occupied_left(array)) { 
          Ant* temp = new Ant(array, x, y-1); 
          colony.push_back(temp); 
        } 
        this->count = 0; 
      } 

    } 
    void die(char array[][20]) { 
      array[this->x][this->y] = '.'; 
    } 
}; 

void moveAnts(char step[][20], std::vector<Ant*> colony) { 

    std::vector<Ant*>::iterator itr; 
    for(itr = colony.begin(); itr < colony.end(); ++itr) { 
      Ant* temp = *itr; 
      temp->move(step); 
    } 
} 

void breedAnts(char step[][20], std::vector<Ant*> colony) { 

    std::vector<Ant*>::iterator itr; 
    for(itr = colony.begin(); itr < colony.end(); ++itr) { 
      Ant* temp = *itr; 
      temp->breed(step, colony); 
    } 
} 

void killAnts(char step[][20], std::vector<Ant*> colony) { 

    std::vector<Ant*>::iterator itr; 
    for(itr = colony.begin(); itr < colony.end(); ++itr) { 
      Ant* temp = *itr; 
      temp->die(step); 
    } 
} 

int main() { 

    srand(time(NULL)); 

    char step[20][20]; 

    for(int i = 0; i < 20; i++) { 
      for(int j = 0; j < 20; j++) { 
        step[i][j] = '.'; 
      } 
    } 

    std::vector<Ant*> colony; 

    for(int i = 0; i < 6; i++) { 
      Ant* test = new Ant(step); 
      colony.push_back(test); 
    } 

    for(int i = 0; i < 4; i++) { 
      print(step); 
      moveAnts(step, colony); 
      breedAnts(step, colony); 
    } 

    killAnts(step, colony); 

    print(step); 

    return 0; 
} 
+0

將元素添加到一個載體可以現有的迭代器無效到載體中。通過索引迭代或使用列表。 – Brian

+0

你有很多泄漏。是否有一個原因,你不想使用'std :: vector '而不是'std :: vector '?無論如何,不​​要忘記在程序結束之前刪除每一隻螞蟻。 – Brandon

+0

一旦基本功能完成,我必須在開始時創建100個Ant對象和5個Doodlebug對象。我找不到任何其他方法,而不是用new創建指向對象的指針。 – Megan

回答

3

更改功能,從而利用向量的參考。你似乎通過價值傳遞了它們。例如,改變

void moveAnts(char step[][20], std::vector<Ant*> colony) 

void moveAnts(char step[][20], std::vector<Ant*>& colony)