gpt4 book ai didi

c++ - 在另一个类中创建类对象数组

转载 作者:行者123 更新时间:2023-11-28 03:53:20 24 4
gpt4 key购买 nike

我有一个问题。我正在尝试创建一个从基类派生的不同类对象的数组。这是代码:

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

现在我在 main 中创建了一个 ListaProduse 实例。我调用 ListaProduse.adauga(filein),然后调用 ListaProduse.afisare()。问题是,当我调用 afisare() 时,程序停止工作并说:

Unhandled exception at 0x00ba3055 in test.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.

指向

this->vector[nrProduse]->afisCamp();

可能是什么问题?谁能帮我?谢谢!

最佳答案

嗯,至于你目前问题的真正原因,我不知道。但我确实知道一件事:您可能正在编写 C++,但使用的是 char* 和其他邪恶的 C 语言。

使用 std::stringstd::vector 等,我向您保证,您的问题会很快解决。

关于c++ - 在另一个类中创建类对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4640017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com