简体   繁体   English

“向量下标超出范围”

[英]“vector subscript out of range”

I have a strange issue with 2 arrays of "pointer to pointer to objects" (by looking a te the code am i getting this right?) I did read A LOT to try and grasp as much knowledge about what I'm trying to do but this error is getting overwhelming and I really wanna find out why is this happening. 我有一个2个数组的“指向对象的指针”的奇怪问题(通过查看代码我是否正确了?),我确实读过很多文章,试图掌握关于我要做什么的知识但是这个错误变得势不可挡,我真的想找出原因。

For what I can see from the error that pops on this line: 对于我可以从此行上弹出的错误中看到的内容:

vec_cliente[i] = new Cliente(nombre,apellido,carne,fecha); //this pops the error

is that I am trying to put the a new Cliente at vec_cliente[i] (0) and that doesn't exist. 是我正在尝试将新的Cliente放在vec_cliente [i](0)处,但不存在。

however... this 但是...这

vec_solicitudes[i] = new Casa_Profesor(carne, fecha_sol, costo_mant, costo_alquiler, cant_p,salon); 

runs fine o.0 运行良好o.0

Principal.h has this: Principal.h具有:

#pragma once
#include string>
#include vector>
#include "Cliente.h"
#include "Persona.h"
#include "Casa_Profesor.h"
using namespace std;

class Principal{


private: 
    Cliente ** vec_cliente;
    Casa_Profesor ** vec_solicitudes;


public:
    static string  info [];
    static string  miembros [];

    Principal(void);
    void cargar(void);
    void calular_mayor_solicitudes(Cliente* vec_cliente, Casa_Profesor* vec_solicitudes);
    void mayor_costo(void);
    void ingreso_total(void);
    void espacio_mayor_uso(void);
    virtual ~Principal(void);

};

Principal.cpp has this: Principal.cpp具有:

#include "StdAfx.h"
#include "Principal.h"
#include iostream>
#include string>
#include sstream>
#include vector>
#include iterator>


using namespace std;

string Principal::info[] = {"623 25-05-2010 625.00 850.50 190 1"

                                ,"199 24-01-2009 300 900.5 180 1"
                ,"199 24-01-2010 300.50 900 175 2"
                ,"201 24-01-2009 300 900.5 180 3"
                ,"201 24-01-2010 300.50 900 175 3"
                ,"201 24-01-2009 30 900.5 180 3"
                ,"201 24-01-2010 300.50 900 175 3"
                ,"404 24-01-2009 300 900.5 180 3"
                ,"404 24-01-2010 300.50 900 175 2"
                ,"404 24-01-2009 30 900.5 180 3"
                ,"404 24-01-2010 300.50 900 175 2"
                ,"404 24-01-2009 300 90.5 180 3"
                ,"505 24-01-2010 300.50 900 175 2"
                ,"505 24-01-2009 300 900.5 180 3"
                ,"505 24-01-2010 300.50 900 175 2"
                ,"505 24-01-2009 300 90.5 180 1"
                ,"505 24-01-2010 300.50 900 175 1"
                ,"505 24-01-2009 300 900.5 180 3"
                ,"505 24-01-2010 1300.50 900 175 2"
                ,"106 24-01-2009 300 900.5 180 3"
                ,"106 24-01-2010 300.50 900 175 3"
                ,"106 24-01-2009 300 900.5 180 3"
                ,"106 24-01-2010 300.50 900 175 3"};


string Principal::miembros[] = {"Jaime Perez 623 22 12 1998"

                ,"Maria Perez 199 02 12 1988"
                ,"Jose Castro 201 3 11 2008"
                ,"Juan Caceres 404 21 1 2007"
                ,"Carla Vivas 505 18 09 1990"
                ,"Daniela Ochoa 106 02 01 2010"};

Principal::Principal(void)
{


}


void Principal::cargar(){

int i = 0, carne = 0, cant_p = 0, salon = 0;
string nombre, apellido, fecha, aux, fecha_sol;
double costo_mant, costo_alquiler;  

vec_solicitudes = new  Casa_Profesor * [(sizeof(info)/sizeof(string))*sizeof(Casa_Profesor)];

    for (i = 0; i < (sizeof(info)/sizeof(string)); i++){
        istringstream stream(info[i],ios_base::in);

        stream >> aux;
        carne = atoi(aux.c_str());
        stream >> fecha_sol;
        stream >> aux;
        costo_mant = atof(aux.c_str());
        stream >> aux;
        costo_alquiler = atof(aux.c_str());
        stream >> aux;
        cant_p = atoi(aux.c_str());
        stream >> aux;
        salon = atoi(aux.c_str());


        vec_solicitudes[i] = new Casa_Profesor(carne, fecha_sol, costo_mant, costo_alquiler, cant_p,salon);  //THIS runs fine 
    }

vec_cliente = new  Cliente * [(sizeof(miembros)/sizeof(string))*sizeof(Cliente)];

    for (i = 0; i < (sizeof(miembros)/sizeof(string)); i++){
        istringstream stream(miembros[i],ios_base::in);

        stream >> nombre;
        stream >> apellido;
        stream >> aux;
        carne = atoi(aux.c_str());
        stream >> fecha;
        fecha.append(" ");
        stream >> aux;
        fecha.append(aux);
        fecha.append(" ");
        stream >> aux;
        fecha.append(aux);

        vec_cliente[i] = new Cliente(nombre,apellido,carne,fecha);  //THIS trows the exception
    }

}

/* the only difference between Casa_Profesor and Cliente is that Cliente inherits from a class Persona and Casa_Profesor doesn't. / * Casa_Profesor和Cliente之间的唯一区别是Cliente继承自Persona类,而Casa_Profesor没有。 as for why is this code so weird: it's a practice for programing but feel free to point out any other programming mistakes I'm making in this code (specially on the sizeof stuff) ALSO, I could edit and post all the code for the clases and all that but I wanted to first see if someoe could point me into an obvious newbie mistake I might be making... thanks in advance for any help */ 至于为什么这段代码这么怪异:这是一种编程实践,但是可以随时指出我在此代码中犯的任何其他编程错误(特别是在sizeof上),我也可以编辑并发布该代码的所有代码。 clases和其他所有东西,但是我想首先看看someoe是否可以指出我可能犯的一个明显的新手错误...在此先感谢您的帮助* /

I am not sure why you get "vector subscript out of range" when you aren't using any vectors, just raw pointers and arrays. 我不确定为什么当您不使用任何向量,仅使用原始指针和数组时,为什么会得到“向量下标超出范围”。 I'll assume that you didn't tell us something! 我以为您没有告诉我们什么!

So I'll just make some random comments on your code: 因此,我将对您的代码进行一些随机注释:

vec_solicitudes = new  Casa_Profesor * [(sizeof(info)/sizeof(string))*sizeof(Casa_Profesor)];

Are you trying to create 1 Casa_Profesor * in vec_solicitudes for each string in info? 您是否要在vec_solicitudes中为信息中的每个字符串创建1个Casa_Profesor *? If so, you want: 如果是这样,您要:

vec_solicitudes = new  Casa_Profesor * [sizeof(info)/sizeof(string)];

This will allocate enough memory for (sizeof(info)/sizeof(string)) (Casa_Profesor *). 这将为(sizeof(info)/ sizeof(string))(Casa_Profesor *)分配足够的内存。

Likewise: 同样地:

vec_cliente = new  Cliente * [sizeof(miembros)/sizeof(string)];

However, your allocations are too big, rather than too small, so they are not the cause of the exception. 但是,您的分配太大而不是太小,因此它们不是异常的原因。 I suggest instead that you look at the Casa_Profesor and Cliente constructors - they are where the exception probably originates. 我建议您改为查看Casa_Profesor和Cliente构造函数-它们可能是异常发生的地方。

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

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