“矢量下标超出范围”

时间:2011-08-04 16:10:02

标签: c++ arrays

我有一个奇怪的问题,有2个“指向对象指针”的数组(通过查看代码,我得到了这个吗?) 我确实阅读了很多尝试并尽可能多地掌握我正在尝试做的事情,但这个错误正在变得无法抗拒,我真的想知道为什么会发生这种情况。

我可以从此行上弹出的错误中看到:

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

是我试图将新的Cliente放在vec_cliente [i](0)并且不存在。

然而......这个

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

运行正常o.0

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有这个:

#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
    }

}

/ * Casa_Profesor和Cliente之间的唯一区别是Cliente继承自Persona类,而Casa_Profesor则没有。   至于为什么这段代码如此奇怪:这是编程的一种做法,但我可以随意指出我在这段代码中犯的任何其他编程错误(特别是关于sizeof的东西)  此外,我可以编辑和发布所有的代码和所有这些,但我想先看看someoe是否可以指出我可能正在制造一个明显的新手错误...... 提前感谢任何帮助* /

1 个答案:

答案 0 :(得分:1)

当你不使用任何向量,只是原始指针和数组时,我不确定为什么你会得到“向量下标超出范围”。我假设你没告诉我们什么!

所以我会对你的代码做一些随机评论:

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

您是否尝试在vec_solicitudes中为info中的每个字符串创建1个Casa_Profesor *?如果是这样,你想要:

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

这将为(sizeof(info)/ sizeof(string))(Casa_Profesor *)分配足够的内存。

同样地:

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

但是,您的分配太大,而不是太小,因此它们不是异常的原因。我建议您查看Casa_Profesor和Cliente构造函数 - 它们可能是异常的起源。

相关问题