"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.
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.
however... this
vec_solicitudes[i] = new Casa_Profesor(carne, fecha_sol, costo_mant, costo_alquiler, cant_p,salon);
runs fine o.0
Principal.h has this:
#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:
#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. 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 */
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? 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 *).
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.
精彩评论