首页 > 解决方案 > 为什么我的断言失败,向量下标超出范围

问题描述

Visual Studio 说我没有问题,但是,每次我尝试运行我的代码时,我都会收到一个断言失败错误,并且错误说向量下标超出范围,我应该怎么做才能解决这个问题,我真的不知道我该怎么做我做错了。

#include <iostream>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <vector>
using std::cin;
using std::cout;
using std::endl;

class Human
{
private:
    int ap;
    int hp;
public:
    Human()
    {
        srand(time(NULL));
        ap = rand() % 3 + 8;
        hp = rand() % 2 + 9;
    }
    int getAp() { return ap; }
    int getHp() { return hp; }
    void dmg(int x) { hp -= x; }
    
};

class Skeleton
{
private:
    int ap;
    int hp;
public:
    Skeleton()
    {
        srand(time(NULL));
        ap = rand() % 3 + 3;
        hp = rand() % 2 + 4;
    }
    int getAp() { return ap; }
    int getHp() { return hp; }
    void dmg(int x) { hp -= x; }

};

class game
{
private:
    std::vector<Human*> hum;
    std::vector<Skeleton*> ske;
    bool adv;
    int sC;
    int hC;
public:
    game(int h, int s)
    {
        srand(time(NULL));
        adv = rand() % 2;
        sC = s;
        hC = h;
        for (int i = 0; i < h; i++) { hum.push_back( new Human()); }
        for (int i = 0; i < h; i++) { ske.push_back(new Skeleton()); }
    }
    ~game()
    {
        for (int i = 0; i < hum.size(); i++)
        {
            Human* current = hum.back();
            hum.pop_back();
            delete current;
        }
        for (int i = 0; i < ske.size(); i++)
        {
            Skeleton* current = ske.back();
            ske.pop_back();
            delete current;
        }
    }
    void start()
    {
        
        int x = hC-1;
        int y = sC-1;
        bool quit = false;
        while (!quit)
        {
            if (adv)
            {
                ske[y]->dmg(hum[x]->getAp());
                if (ske[y]->getHp() <= 0) { y--; ske.pop_back();}
                adv = 0;
            }
            if (!adv)
            {
                hum[x]->dmg(ske[y]->getAp());
                if (hum[x]->getHp() <= 0) { x--; hum.pop_back(); }
                adv = 1;
            }
            if (hum.size() == 0 || ske.size() == 0)
            {
                cout << "human left : " << hum.size() << "skeleton left : " << ske.size();
                quit = true;
            }
        }
    }
};

int main()
{
    game g1(10, 5);
    g1.start();

    return 0;
}

我总是得到这个错误框。

这是错误消息,我不知道他们在说什么。

我也明白了,这是什么?

这个

我已将其修复为您所有的评论,这是完整的代码,它可能看起来像垃圾,但它运行

#include <iostream>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <vector>
using std::cin;
using std::cout;
using std::endl;

class Human
{
private:
    int ap;
    int hp;
public:
    Human()
    {
        ap = rand() % 3 + 8;
        hp = rand() % 2 + 9;
    }
    int getAp() { return ap; }
    int getHp() { return hp; }
    void dmg(int x) { hp -= x; }
    
};

class Skeleton
{
private:
    int ap;
    int hp;
public:
    Skeleton()
    {
        ap = rand() % 3 + 3;
        hp = rand() % 2 + 4;
    }
    int getAp() { return ap; }
    int getHp() { return hp; }
    void dmg(int x) { hp -= x; }

};

class game
{
private:
    std::vector<Human*> hum;
    std::vector<Skeleton*> ske;
    bool adv;
    int sC;
    int hC;
public:
    game(int h, int s)
    {
        srand(time(NULL));
        adv = rand() % 2;
        sC = s;
        hC = h;
        for (int i = 0; i < h; i++) { hum.push_back( new Human()); }
        for (int i = 0; i < s; i++) { ske.push_back(new Skeleton()); }
    }
    ~game()
    {
        for (int i = 0; i < hum.size(); i++)
        {
            Human* current = hum.back();
            hum.pop_back();
            delete current;
        }
        for (int i = 0; i < ske.size(); i++)
        {
            Skeleton* current = ske.back();
            ske.pop_back();
            delete current;
        }
    }
    void start()
    {
        
        int x = hum.size()-1;
        int y = ske.size()-1;
        bool quit = false;
        while (!quit)
        {
            if (hum.size() == 0 || ske.size() == 0)
            {
                cout << "human left : " << hum.size() << "skeleton left : " << ske.size();
                quit = true;
                break;
            }
            if (adv)
            {
                ske.at(y)->dmg(hum.at(x)->getAp());
                if (ske.at(y)->getHp() <= 0) { y--; ske.pop_back();}
                adv = 0;
            }
            else if (!adv)
            {
                hum.at(x)->dmg(ske.at(y)->getAp());
                if (hum.at(x)->getHp() <= 0) { x--; hum.pop_back(); }
                adv = 1;
            }
            
        }
    }
};

int main()
{
    game g1(8, 20);
    g1.start();

    return 0;
}

标签: c++

解决方案


您的最终代码比应有的复杂,我通过以下方式对其进行了简化:

  • 去掉windows相关头文件,不使用
  • 使用成员初始化器列表
  • 修改dmg返回最新的hp
  • 删除 , 中不必要的索引start以使用back
  • 去掉指针的向量,这里可以使用对象
  • srand调用移入main(实际上我们可以在这里使用 std::random )
  • 简化while循环start
#include <ctime>
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;

class Human {
 private:
  int ap;
  int hp;

 public:
  Human() : ap(rand() % 3 + 8), hp(rand() % 2 + 9) {}
  int getAp() { return ap; }
  int getHp() { return hp; }
  int dmg(int x) { return hp -= x; }
};

class Skeleton {
 private:
  int ap;
  int hp;

 public:
  Skeleton() : ap(rand() % 3 + 3), hp(rand() % 2 + 4) {}
  int getAp() { return ap; }
  int getHp() { return hp; }
  int dmg(int x) { return hp -= x; }
};

class game {
 private:
  bool adv;
  int sC;
  int hC;
  std::vector<Human> hum;
  std::vector<Skeleton> ske;

 public:
  game(int h, int s) : adv(rand() % 2), sC(s), hC(h), hum(h), ske(s) {}

  void start() {
    while (!hum.empty() && !ske.empty()) {
      if (adv) {
        if (ske.back().dmg(hum.back().getAp()) <= 0) {
          ske.pop_back();
        }
      } else {
        if (hum.back().dmg(ske.back().getAp()) <= 0) {
          hum.pop_back();
        }
      }
      adv = !adv;
    }
    cout << "human left : " << hum.size() << ", skeleton left : " << ske.size();
  }
};

int main() {
  srand(time(NULL));
  game g1(8, 20);
  g1.start();

  return 0;
}

在线演示


推荐阅读