简体   繁体   English

调试断言失败。 向量下标超出范围

[英]Debug Assertion failed. Vector subscript out of range

I know that this is an exhausting repeated question, but I really couldn't solve my problem by reading other similar topics.我知道这是一个令人筋疲力尽的重复问题,但我真的无法通过阅读其他类似主题来解决我的问题。 In fact when it reaches to nquad++;事实上,当它达到 nquad++ 时; gives this error.给出这个错误。 Although all the variables are correctly calculated and seemingly stored, I don't know why this error happens?虽然所有变量都正确计算并且看似存储,但我不知道为什么会发生这个错误? Here is a part of my code that causes this error (grid.cpp):这是导致此错误的代码的一部分(grid.cpp):

    nquad = -1;
quad.resize(ny - 1);
for (auto j = 0; j < ny - 1; ++j) {
    for (auto i = 0; i < nx - 1; ++i) {
        quad[i].resize(4);
        inode = i + j * nx;
        i1 = inode;
        i2 = inode + 1;
        i3 = inode + nx + 1;
        i4 = inode + nx;
        //Order the quad counterclockwise:
        nquad = nquad + 1;
        quad[nquad][0] = i1;
        quad[nquad][1] = i2;
        quad[nquad][2] = i3;
        quad[nquad][3] = i4;
    }
}
nquad++;

And this is the grid.h:这是grid.h:

#ifndef GRID_H
#define GRID_H

#include <math.h>       // sqrt
#include <cstring>      //needed for memset
//#include <stdlib.h>     //malloc
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>      // std::istringstream

using std::vector;
using std::string;

class Node_type {
public:
Node_type();
~Node_type() {
    vector<Node_type> nghbr;
    vector<Node_type>* w1 = new vector<Node_type>();
    delete w1;
}
int nnghbrs, nelms;                   //number of neighbors & elements
double x, y;
vector<int> elm, nghbr;               //list of elements
};

class Elm_type {
public:
Elm_type();
~Elm_type() {
    vector<Elm_type> vtx, nghbr;
    vector<Elm_type>* w2 = new vector<Elm_type>();
    delete w2;
}
int nvtx, nnghbrs;                   //number of vertices & neighbors
//vector<vector<int>> nghbr;
vector<int> vtx, nghbr;
double x, y;
};

class Grid {
public:
Grid();
size_t i, j;
int nx, ny, nnodes, inode, ntria, nquad, nelms;
double xmin, xmax, ymin, ymax, dx, dy;
vector<vector<double>> xs, ys;
vector<double> xn, yn;
vector<vector<int>> quad;
int x1, x2, x3, x4;
~Grid() {
    vector<Grid> xs, ys, quad, x, y;
    vector<Grid>* x5 = new vector<Grid>();
    delete x5;      //pointer
}
void nodes(double, double, double, double, int, int);
void grid_file(const string& ofile);
void read_grid(const string&);
void construct_grid();
void plot_grid(const string& datafile);
Node_type* node = new Node_type;
//node = (Node_type*) malloc(sizeof(Node_type));
//delete node;
Elm_type* elm = new Elm_type;
//elm = (Elm_type*) malloc(sizeof(Elm_type));
//delete elm;
//Elm_type* elm;
};

#endif

After a little change my code is functioning.稍作改动后,我的代码就可以运行了。 So, I post it here:所以,我把它贴在这里:

    nquad = -1;
quad.resize(ny - 1);
quad.push_back(vector<int>());
for (auto j = 0; j < ny - 1; ++j) {
    for (auto i = 0; i < nx - 1; ++i) {
        quad[i].resize(4);
        inode = i + j * nx;
        i1 = inode;
        i2 = inode + 1;
        i3 = inode + nx + 1;
        i4 = inode + nx;
        //Order the quad counterclockwise:
        nquad = nquad + 1;
        quad[0].push_back(i1);
        quad[1].push_back(i2);
        quad[2].push_back(i3);
        quad[3].push_back(i4);
    }
}
nquad++;

} //end function nodes } //结束function节点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM