Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3667020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:57:01+00:00 2026-05-19T01:57:01+00:00

I have a problem. I’m trying to create an array of different class objects

  • 0

I have a problem. I’m trying to create an array of different class objects derived from a base class. This is the code:

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

Now, I’m trying to create from this class an array of CD and 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();
};

and this is the code for ListaProduse class:

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

Now I create in main an instance of ListaProduse. I call ListaProduse.adauga(filein) and then ListaProduse.afisare(). The problem is that when I call afisare() the program stop working and says:

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

pointing to

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

What could be the problem? Can anyone help me? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:57:02+00:00Added an answer on May 19, 2026 at 1:57 am

    Thank you very much. The problem was on

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

    It was supposed to be

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

    My mistake. 🙁 And the idea with std::vector is more than great, but I have to do my project (for school) using polymorphism.

    Thank you again!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I have problem when I try insert some data to Informix TEXT column via
I do not have problem as such but I am quite new to Ruby.
I have a problem using the Java search function in Eclipse on a particular
I have a problem with a little .Net web application which uses the Amazon
I have a problem in my project with the .designer which as everyone know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.