c++ - 为什么我在这里得到一个 vector 下标超出范围的错误?

标签 c++ vector

我正在创建某种 rpg 战斗,其中程序从 .txt 文件读取输入。我创建了代码,但是当我想开始战斗时,它给了我一个超出范围的错误 vector 下标。谁能帮我解决这个问题?非常感谢 :) 这是代码。我包含了所有内容,以便您可以获得完整的上下文,但我认为主要问题出在主 cpp 中的 while 循环中,如果您想直接跳到那里。

所以我们在同一条轨道上,lamanite(生命值和恢复点)的 txt 文件的内容是

8 2

7 3

6 1

对于软 Jade

10 3

12 4

11 5

这是我的 warrior.h 文件

#pragma once
#include <string>

using namespace std;

class warrior
{
public:

    warrior ();
    warrior (int h, int r);
    int getDamage() const;
    void takeDamage(int damage);
    int getCurrentHP() const;
    void regenerate();
    string tostring(int h, int r);

private:
    int HitPoints;
    int RegPoints;
    int damage;


};

这是我的战士cpp

#include "warrior.h"
#include <string>
#include <iostream>

warrior::warrior(int h, int r)
{
    HitPoints = h;
    RegPoints = r;
}

int warrior::getDamage() const
{
    int damage = rand () % HitPoints;
    return damage;
}

void warrior::takeDamage(int damage)
{
    HitPoints = HitPoints - damage;
}

int warrior::getCurrentHP() const
{
    return HitPoints;
}

void warrior::regenerate() 
{
    HitPoints = HitPoints + rand () % (RegPoints);
}

string warrior::tostring(int h, int r)
{
    return 0;
}

我的主文件

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <ctime>

#include "warrior.h"

using namespace std;

void main ()
{
    srand(time(0));
    ifstream input1;
    cout << "input file name nephite: ";
    string filename;
    cin >> filename;

    input1.open(filename);

    int HP1, RP1;
    vector <warrior*> nephites;

    while (input1 >> HP1 >> RP1)
    {
        nephites.push_back(new warrior(HP1, RP1));
    }

    cout << nephites.size() << endl;

    ifstream input2;
    cout << "input file name lamanite : ";
    string filename2;
    cin >> filename2;

    input2.open(filename2);
    int HP2, RP2;
    vector <warrior*> lamanites;

    while (input2 >> HP2 >> RP2)
    {
        lamanites.push_back(new warrior(HP2, RP2));
    }
    cout << lamanites.size() << endl;

    cout << endl << "Battle" << endl;

    warrior nephitesw  = warrior (HP1,RP1);
    warrior lamanitesw = warrior (HP2,RP2);

    while ((nephites.size() > 0) && (lamanites.size() > 0))
    {

        int rN = rand () % nephites.size();
        int rL = rand () % lamanites.size();
        cout << rN << "xx" << rL << endl; // so that i know what rN and rL is

        while((nephites[rN]->getCurrentHP() > 0) && (lamanites[rL]->getCurrentHP() > 0)) // the program can't execute this part of the code
        {
            nephites[rN]->takeDamage(lamanites[rL]->getDamage());
            lamanites[rL]->takeDamage(nephites[rN]->getDamage());

            if(lamanites[rL]->getCurrentHP() > 0)
            {
                lamanites[rL]->regenerate();
            }
            else
            {
                lamanites.erase(lamanites.begin() + (rL));
            } 

            if(nephites[rN]->getCurrentHP() > 0) 
            {
                nephites[rN]->regenerate();
            } 
            else
            {
                nephites.erase(nephites.begin() + (rN));
            }
        }

        cout << "NEP HP: " << nephites[rN]->getCurrentHP() << " " << "LAM HP: " << lamanites[rL]->getCurrentHP() << endl;
    }

    system ("Pause");
}

最佳答案

您有一个 while 循环来测试 nephites[rN]lamanites[rL] 的某些属性:

while((nephites[rN]->getCurrentHP() > 0) && (lamanites[rL]->getCurrentHP() > 0)
{
    // ...
}

但在该循环中,您可能会删除这些元素:

{lamanites.erase(lamanites.begin() + (rL));} 

// ...

{nephites.erase(nephites.begin() + (rN));}  

至少,在这些删除操作之一之后,您将在下一次循环迭代中测试不同的软软 Jade 或 lamanite 对象(这可能是您想要的,也可能不是您想要的),但如果您删除了最后一个容器中的元素,您会遇到索引现在超出范围的问题。

关于c++ - 为什么我在这里得到一个 vector 下标超出范围的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906065/

相关文章:

android - 如何在安卓上使用QFile?

c++ - Windows 路径处理库

ios - 如何在 spriteKit 中以特定角度施加力?

c++ - 如何定义具有循环依赖性的 C++ 类?

c++ - 列出名为 boost 的托管共享内存的名称

c++ - uuid_t 的现有哈希函数

c# - 如何在 C# 中获取向量类型?

algorithm - 从向量的大小中减去一个常数(缩短向量)而不使用平方根?

html - CSS/HTML 是如何在浏览器中呈现的?

c++ - 如何从 vector 初始化 valarray?