because it marks me error in int main saying no matching function FrequenciesCardiac?

1
//Frecuencia Cardiaca
#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

class FrecuenciasCardiacas
{
private: //Atributos//
    string nombrePersona;

public: //Metodos//
    FrecuenciasCardiacas(string nombre)//Constructor
    {
        nombrePersona=nombre;
    }

void establecerNombrePersona(string nombre)//nombre
    {

        cout<<"Introduzca nombre: ";
        cin>>nombrePersona;
        nombrePersona = nombre;
    }

string obtenerNombrePersona() const
    {
        return nombrePersona;
    }

};

int main()
{
    FrecuenciasCardiacas ob1;
    ob1.obtenerNombrePersona();

    return 0;
}
    
asked by user103440 14.10.2018 в 00:47
source

2 answers

4

When you try to create the object, you try to invoke the default constructor:

FrecuenciasCardiacas ob1;

It is known that it is the default constructor because you are not passing any argument to the constructor. However, the class FrecuenciasCardiacas does not implement said constructor, but a custom one:

FrecuenciasCardiacas(string nombre)//Constructor
{
    nombrePersona=nombre;
}

This constructor disables the constructor by default unless you expressly implement it, which does not happen.

What happens then is that the program should call

FrecuenciasCardiacas::FrecuenciasCardiacas()

But that constructor is not available.

At this point there are two possible solutions:

Universal solution : Implement the default constructor:

class FrecuenciasCardiacas
{
public:
      FrecuenciasCardiacas()
      { }
};

If you are with C ++ 11 or higher you can use default :

class FrecuenciasCardiacas
{
public:
      FrecuenciasCardiacas() = default;
};

Customized solution : Removes the custom constructor. This solution is only viable if said constructor is not necessary at all. By not implementing any custom constructor the compiler will create the default constructor transparently:

class FrecuenciasCardiacas
{
private: //Atributos//
    string nombrePersona;

public: //Metodos//

    void establecerNombrePersona(string nombre)//nombre
    {

        cout<<"Introduzca nombre: ";
        cin>>nombrePersona;
        nombrePersona = nombre;
    }

    string obtenerNombrePersona() const
    {
        return nombrePersona;
    }
};
    
answered by 14.10.2018 в 02:26
0

I have done it in the following way, I usually do the classes in this way. I tried it and it worked for me, I do not know if the operation will be the one you want, but here I leave the code:

#include <iostream>

using namespace std;

class FrecuenciasCardiacas{

private: //Atributos

    string nombrePersona;

public: //Métodos

    FrecuenciasCardiacas(string); //Constructor

    void EstablecerNombrePersona(); //Método

};

FrecuenciasCardiacas::FrecuenciasCardiacas(string _nombrePersona){ //Inicializamos los atributos

    nombrePersona = _nombrePersona;

}

void FrecuenciasCardiacas::EstablecerNombrePersona(){

    cout << "Introduzca el nombre: ";
    cin >> nombrePersona;
    cout << "El nombre es: " << nombrePersona;
}

int main(){

    FrecuenciasCardiacas p1("Prueba");

    p1.EstablecerNombrePersona();


    return 0;
}

As you can see, to initialize the attributes I use FrecuenciasCardiacas::FrecuenciasCardiacas(string _nombrePersona) . Instead of creating it within the class as you have done it. With this we get that when creating the object of the class, make reference to that constructor.

Below, when creating the object, it is important to pass some parameters to it by default, otherwise it will give us an error when compiling: FrecuenciasCardiacas p1("Prueba") . It does not matter what value you put at the beginning because then when you call the method, you will change it for the one that the user has entered.

I hope it serves you. If there is an error, do not hesitate to tell me. Greetings.

    
answered by 14.10.2018 в 12:46