我有个问题。我正在尝试创建一个从基类派生的不同类对象的数组。这是代码:
class Cdvd{
protected:
char *numeArtist; //numer artist sau film
char *tip;// CD sau DVD
int cantitate; //numar de CD/DVD din baza de date
int pret; //pret per unitate
int cod_intern; //cod intern produs. Spre exemplu 0 pentru un film horror.
void scriereInFisier(std::fstream &out); //scriere in fisierul cu produse
public:
Cdvd(); //constructor produs implicit
Cdvd(const Cdvd &cdvd); //constructor produs cu parametru alt produs
Cdvd(char *artist, char *tip, int cant, int pret, int cod_int); //constructor produs cu parametri
~Cdvd(); //destructor produs
char *getNume(); //returneaza pointer la numele artistului sau filmului
void parseFis(char *artist, char *tip, int cant, int pret, int cod_int);
virtual void afisCamp()=0; //afiseaza campurile produsului
};
class CD : public Cdvd{
private:
char *genMuzica; //genul muzicii
char *extensie; //extensia fisierelor audio
int nrTrackuri; //numar melodii
int lungime; //lungime totala melodii
public:
CD(); //constructor implicit cd
CD(char *artist, int cant, int pret, int cod_int, char *genMuzica, char *extensie, int nrTrackuri, int lungime);
CD(const CD &cd); //constructor cd cu parametru alt cd
~CD(); //destructor cd
void parseFis(char *artist, int cant, int pret, int cod_int, char *genMuzica, char *extensie, int nrTrackuri, int lungime);
void afisCamp(); //afiseaza campurile cd-ului
};
class DVD : public Cdvd{
private:
char *genFilm;
char *extensie;
int lungime;
public:
DVD();
DVD(char *artist, int cant, int pret, int cod_int, char *genFilm, char *extensie, int lungime);
~DVD(){cout<<"~DVD()"<<endl;};
void parseFis(char *artist, int cant, int pret, int cod_int, char *genFilm, char *extensie, int lungime);
void afisCamp();
};
现在,我正在尝试从此类创建CD和DVD阵列:
class ListaProduse{
static const int dimMax=20; //dimensiune maxima lista produse
Cdvd **vector; //vector in care se retin produsele
int nrProduse; //retine cate produse sunt in baza de date
public:
ListaProduse();
~ListaProduse();
void adauga(std::fstream &in);
void afisare();
};
这是ListaProduse类的代码:
ListaProduse::ListaProduse()
{
nrProduse=0;
vector=new Cdvd*[dimMax];
}
ListaProduse::~ListaProduse()
{
if(nrProduse>0)
{
delete vector;
}
}
void ListaProduse::adauga(std::fstream &in)
{
if(in.is_open())
{
int pret,cant,cod;
char temp1[20],aux1[20];
char aux;
while(!in.eof())
{
in>>aux;
in>>temp1;
char *teemp1=new char[strlen(temp1)+1];
strcpy(teemp1,temp1);
in>>pret;
in>>cant;
in>>cod;
switch(aux){
case 'C':{
int nrTr,lung;
in>>aux1;
char *auxiliar1=new char[strlen(aux1)+1];
strcpy(auxiliar1,aux1);
in>>aux1;
char *auxiliar2=new char[strlen(aux1)+1];
strcpy(auxiliar2,aux1);
in>>nrTr;
in>>lung;
CD *x;
x=new CD;
x->parseFis(teemp1,pret,cant,cod,auxiliar1,auxiliar2,nrTr,lung);
vector[nrProduse]=x;
vector[nrProduse]->afisCamp();
//cout<<vector[nrProduse]->numeArtist;
nrProduse++;
break;
}
case 'D':{
int lung;
in>>aux1;
char *auxiliar1=new char[strlen(aux1)+1];
strcpy(auxiliar1,aux1);
in>>aux1;
char *auxiliar2=new char[strlen(aux1)+1];
strcpy(auxiliar2,aux1);
in>>lung;
DVD *x;
x=new DVD;
x->parseFis(teemp1,pret,cant,cod,auxiliar1,auxiliar2,lung);
vector[nrProduse]=x;
vector[nrProduse]->afisCamp();
nrProduse++;
break;
}
};
}
}
}
void ListaProduse::afisare()
{
if(nrProduse>0)
for(int i=0;i<nrProduse;i++)
{
this->vector[nrProduse]->afisCamp();
}
else
cout<<"Nu exista produse in baza de date!"<<endl;
}
现在,我主要创建一个ListaProduse实例。我先调用ListaProduse.adauga(filein),然后再调用ListaProduse.afisare()。问题是,当我调用afisare()时,程序停止工作并显示:
未处理的0x00ba3055中的异常
test.exe:0xC0000005:访问冲突
读取位置0xcdcdcdcd。
指向
this-> vector [nrProduse]-> afisCamp();
可能是什么问题呢?谁能帮我?谢谢!
最佳答案
好吧,关于您当前问题的实际原因,我不知道。但是我确实知道一件事:您大概是在编写C ++,但是使用char*
和其他邪恶的C-ism。
使用std::string
,std::vector
等,我向您保证您的问题将很快解决。