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

closed . this question needs details or clarification . He's not getting answers right now.

want to improve this question? Add details and clarify the problem by editing this post .

Closed 4 years ago .

Improve this question

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;
}
 0
Author: PaperBirdMaster, 2016-06-29

1 answers

Your whole 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 with 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 their constructor require a Ficha a int and a string:

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

The Integer and the string have a parameter by default but the parameter Ficha is required... Where Are you passing it to every one of the n instances created? Nowhere, that's the mistake.

Solutions

You have 3 solutions to your problem.

Creates a parameterless constructor.

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++compiler 11 or higher, you can tell the compiler to generate the default code for the parameterless constructor:

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

Usa std::vector<Cine>.

std::vector it has a constructor in which you can indicate how many elements you want to build at once and it takes as model an already built element, 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 Cine after of building it (rather than at the time of construction) why would you do you give a constructor with parameters?, removes such a 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.
  • You Abuse std::endl, which is usually not a good idea, I advise you to take a look at this thread for more details.
  • in the loops for use pre-increment whenever possible.
 4
Author: PaperBirdMaster, 2017-09-18 06:59:07