小さなDOSベースのゲーム(コースプロジェクト)でメークシフトタイマーとして静的変数を使用しています。この変数は、状態効果が消える前のターン数を記録します。 静的int変数のstd :: out_of_range
for (auto &i : v) // <-- code that calls enemyAttack
enemyAttack(i, p, str, i.attack);
break;
void enemyAttack(Enemy &e, playerObject &p, std::array<std::string, NUM_MESSAGES> &str, void(*a)(playerObject &p, std::array<std::string, NUM_MESSAGES> &str)) {
int die = rand() % 100 + 1;
int d = 1;
a(p, str); // <-- Call function which causes the error
...
}
void batAttack(playerObject &p, std::array<std::string, NUM_MESSAGES> &str) {
static int time = 2;
static bool bit = false;
if (rand() % 10 < CHANCE_OF_STATUS_EFFECT && !bit) {
p.damage /= 2;
str[STATUS] += "WEAKENED ";
bit = true;
}
else if (time == 0) {
p.damage *= 2;
str[STATUS].replace(str[STATUS].find("WEAKENED ", 0), 9, "");
time = 2; // <-- error
bit = false;
}
else if (bit) {
time--;
}
}
が、私は第二の条件内側の線
time = 2;
でのstd :: out_of_rangeエラーが表示されます。ここでは、コードです。この関数は、プライマリ攻撃関数から関数ポインタを介して呼び出されます。エラーはランダムであるように見え、MSVSはエラーが発生したときに必要な値を持つすべての変数を報告します。
「STATUS」とは何ですか? 'NUM_MESSAGES'とは何ですか?彼らの価値は何ですか? [最小、**完全**、および検証可能な例](http://stackoverflow.com/help/mcve)を作成してください。 –
また、 'str [STATUS] .find(" WEAKENED "、0)'があなたが探している文字列を見つけられないとどうなると思いますか?たとえ何かが「決して起こらない」と考えられていても、それはいつもあります! –
この静的変数に同時にアクセスしようとしている複数のスレッドを偶然に持っていませんか? – 9Breaker