通过另一个函数调用带有对象向量的函数

时间:2017-05-20 02:27:35

标签: c++

当我尝试在迭代对象矢量的while循环中调用另一个函数时,我遇到了问题(不一定有错误,但控制台变成空白)。

基本上fightEnemy在我通过while循环运行时不会显示,但是当我不通过enemyComboCheck函数中的while循环运行它时。

我的代码:

int fightEnemy(Player &player, Enemy &enemy) {
    //just for example
    cout << player.name << endl;
    cout << enemy.name << endl;
    return 0;
}

int enemyComboCheck(int id1, int id2, vector<Player> &allPlayers, vector<Enemy> &allEnemies){
    int iteratorForPlayer = 0;
    int iteratorForEnemy = 0;
    id1 = id1 - 1;
    id2 = id2 - 1;

    while(true){
        if(id1 == allPlayers[iteratorForPlayer].id && id2 == allEnemies[iteratorForEnemy].eid) fightEnemy(allPlayers[iteratorForPlayer], allEnemies[iteratorForEnemy]); break;
        if(iteratorForEnemy <= 18) iteratorForEnemy++;
        if(iteratorForPlayer <= 5) iteratorForPlayer++;
    }
    return 0;
}

int main(){
    vector<Player> allPlayers; //pretend this has 5 players
    vector<Enemy> allEnemies; //pretend this has 18 enemies
    enemyComboCheck(uid1, uid2, allPlayers, allEnemies); //this won't print out fightEnemy while in the while loop
    return 0;
}

1 个答案:

答案 0 :(得分:0)

break声明之后隐藏了if。它将始终在第一次迭代期间退出循环,因为它不包含在if块中。