【发布时间】:2011-08-05 00:33:10
【问题描述】:
我对 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 的东西) 另外,我可以编辑并发布所有类的所有代码,但我想先看看是否有人能指出我可能犯的一个明显的新手错误...... 在此先感谢您的帮助 */
【问题讨论】:
-
当你甚至不使用任何向量时,我看不出你如何得到向量下标错误。