No matching function for call to 'Class :: Class ()' [closed]

0

It marks me the error when wanting to reserve memory for 'n' movies

//Main.cpp

introducir el código aquí

#include <iostream>
#include"Pelicula.h"
#include"Ficha.h"
#include"Cine.h"
#include<string.h>
#include<cstdlib>
using namespace std;

int main()
{

   Cine *C;
   int n,i;

   cout<< "Cuantas Peliculas? ";
   cin>>n;

   C = new Cine[n];

   for(i=0; i<n; i++){
    cout<< "Ingrese los datos de la pelicula "<<i+1<<endl;
    C[i].Leer();
    system("cls");
   }
   for(i=0; i<n; i++){
    cout<< "Las peliculas en el cine son: "<<endl;
   C[i].Imprimir();
   }
   delete []C;
    return 0;
}
//Ficha.h 
      #ifndef FICHA_H
#define FICHA_H
#include"Pelicula.h"
#include<string.h>
using namespace std;

class Ficha:public Pelicula
{
public:
    Ficha(string="",string="",int=1, int=60,string="");

    void Leer();
    void CalCosto();
    void Imprimir();

private:
    int Costo;
    string Tipo;

    };

#endif // FICHA_H
//Ficha.cpp
#include "Ficha.h"
#include"Pelicula.h"
#include<iostream>
#include<string.h>
using namespace std;

Ficha::Ficha(string nom,string gen,int dur, int cos,string     tip):Pelicula(nom,gen,dur),Costo(cos),Tipo(tip)
{
//ctor
}
void Ficha::Leer(){
Pelicula::Leer();
cout<< "Escriba el tipo de pelicula(1.- 2D,2.- 3D): ";
getline(cin,Tipo);

}
void Ficha::CalCosto(){
if(Tipo==1){
    cout<< "Costo: 35";
}else if(Tipo==2){
    cout<< "Costo: 60";
}
}
//Cine.h
#ifndef CINE_H
#define CINE_H
#include"Pelicula.h"
#include"Ficha.h"
#include<string.h>
using namespace std;

class Cine
{
public:
    Cine(Ficha,int=1,string="");
    void Leer();
    void Imprimir();

    void modificaTuFichaCine(Ficha);

private:
    Ficha fichacine;
    int NumeroDePeliculas();
    string ArregloDePeliculas();

};

#endif // CINE_H
//Cine.cpp
#include "Cine.h"
#include"Pelicula.h"
#include"Ficha.h"

Cine::Cine(Ficha fi,int numpel,string     arr):NumeroDePeliculas(numpel),ArregloDePeliculas(arr)
{
//ctor nacimiento.modificaTuD(f.dameTuD());

}
void Cine::Leer(){
cout<< "Ingrese cuantas peliculas :";
cin>>NumeroDePeliculas();
fichacine.Leer();

}
void Cine::Imprimir(){
fichacine.Imprimir();
}
//Pelicula.h
#ifndef PELICULA_H
#define PELICULA_H
#include<string.h>
#include<iostream>
using namespace std;

class Pelicula
{
public:
    Pelicula(string="",string="",int=1);

    void Leer();// void pidele
    void Imprimir();    //void muestra

private:
    string NombreDePelicula,Genero;
    int Duracion;
};

#endif // PELICULA_H
//Pelicula.cpp
#include "Pelicula.h"
#include<iostream>
#include<string.h>
using namespace std;

Pelicula::Pelicula(string nom,string gen,int     dur):NombreDePelicula(nom),Genero(gen),Duracion(Duracion)
{
//ctor
}
void Pelicula::Leer(){
cout<< "Ingrese el Nombre de la pelicula: ";
getline(cin,NombreDePelicula);
cout<< "Ingrese el genero: ";
getline(cin,Genero);
cout<< "Escriba la duracion: ";
cin>>Duracion;
}
void Pelicula::Imprimir(){
cout<< "Nombre de la Pelicula: "<<NombreDePelicula<<endl
    << "Genero: "<<Genero<<endl
    << "Duracion: "<<Duracion<<endl;
}
    
asked by Julio Alvarado 29.06.2016 в 07:03
source

1 answer

4

All your problem is reproducible with this simple code:

struct C
{
    C(int){};
};

int main()
{
    int n{};

    std::cout<< "Cuantos n? ";
    std::cin >> n;

    C *c = new C[n]; // error al querer reservar memoria para 'n' C
    return 0;
}

The error is quite clear and you have it in the title itself, but I do not know why its name changed:

You lack default constructor for Cine . When requesting memory for Cine :

C = new Cine[n];

You are building n instances, each of the instances, according to your constructor, require Ficha a int and% string :

Cine(Ficha,int=1,string="");

The integer and the string have a default parameter but the Ficha parameter is required ... Where are you passing it to every one of the n instances created? Nowhere is that the error.

Solutions

You have 3 solutions to your problem.

Create a constructor without parameters.

class Cine
{
public:
    Cine(Ficha,int=1,string="");
    Cine() // <---- constructor sin parametros
    {
            ...
            ...
            ...
    }
    void Leer();
    void Imprimir();

    void modificaTuFichaCine(Ficha);

private:
    Ficha fichacine;
    int NumeroDePeliculas();
    string ArregloDePeliculas();
};

If you have a C ++ 11 compiler or higher, you can tell the compiler to generate the default code for the constructor without parameters:

class Cine
{
public:
    Cine(Ficha,int=1,string="");
    Cine() = default; // <---- constructor por defecto
    void Leer();
    void Imprimir();

    void modificaTuFichaCine(Ficha);

private:
    Ficha fichacine;
    int NumeroDePeliculas();
    string ArregloDePeliculas();
};

Defines a default value for the first parameter of Cine

class Cine
{
public:
    Cine(Ficha={},int=1,string="");
    void Leer();
    void Imprimir();

    void modificaTuFichaCine(Ficha);

private:
    Ficha fichacine;
    int NumeroDePeliculas();
    string ArregloDePeliculas();
};

Use std::vector<Cine> .

std::vector has a constructor in which you can indicate how many elements you want to build from hit and take as model an element already built, so you could do:

int main()
{
    int n{};

    std::cout << "Cuantas Peliculas? ";
    std::cin >> n;

    std::vector<Cine> C(n, Cine{Ficha{}});

    for (int i = 0; i < n; ++i){
        std::cout << "Ingrese los datos de la pelicula " << i + 1 << std::endl;
        C[i].Leer();
        system("cls");
    }
    for(int i = 0; i < n; ++i){
        std::cout << "Las peliculas en el cine son: " << std::endl;
        C[i].Imprimir();
    }

    // No necesitas delete C, "se borra solo".
    return 0;
}

Additional notes.

  • If you are going to fill in the data of Cine after of building it (instead of at the time of construction) why do you give a constructor with parameters ?, remove the constructor.
  • The using namespace std is not a sin, but I advise you to take a look at this thread to know why it is usually advised against.
  • Abuse of std::endl , which is usually not a good idea, I suggest you take a look at this thread to know more details.
  • In loops for use pre-increment whenever it is possible .
answered by 29.06.2016 / 09:13
source