Problem with the Constructor (C ++ Error C2512, no appropriate default constructor available)

4

I need my constructor to start every element of my array in 0.0 , but with my code it tells me that:

Error C2512 'Vendedor': no appropriate default constructor available". 

Annex the code:

#pragma once 
#include <iostream> 
#include <iomanip>
using namespace std;
class Vendedor
{
 public: 
   Vendedor(float Ventas[12]);
   void asignaventas(float);
   void imprimeventas();
 private: 
   float Ventas[12];
   float VentasTotal; 
   float CalVtasTotal(float);  
   float Ventastotal =0; }
#include "Vendedor.h"
#include <iostream> 
using namespace std;

Vendedor::Vendedor(float Ventas[12]) {
    for (int i = 0; i < 12; i++) {
        this->Ventas[i] = 0.0f;
    }
}

void Vendedor::asignaventas(float Ventas) {
    float x = 0;
    cout << "Introdusca la cantidad de ventas realizadas en los 12 meses: "<<endl; 
    for (int i = 0; i < 12; i++) {
        cin >> x; 
        this->Ventas[i] = x;
    } 
} 

void Vendedor::imprimeventas() {
    cout << " Las ventas ascienden a: " << CalVtasTotal(Ventas[12]) << endl;;
}

float Vendedor::CalVtasTotal(float Ventas) {  
    float total = 0;
    for (int i = 0; i < 12; i++) { 
    total = total + this->Ventas[i];
    }  
    this->VentasTotal = total;
    return VentasTotal;
}
#include <iostream> 
#include "Vendedor.h"
using namespace std; 

int main(int argc, char *argv[]) {  
    Vendedor ob; //aquí marca el error
    ob.asignaventas(12); 
    ob.imprimeventas();
    return 0;
}
    
asked by Aheal 21.02.2016 в 05:35
source

2 answers

1

I have not worked with C ++ for a long time, but if I'm not mistaken, the problem is how the constructor is defined and called: right now Vendedor has only one constructor, which takes as an input parameter an array of float with size 12:

Vendedor::Vendedor(float Ventas[12])

But when the object is declared, it is done this way:

Vendedor ob;

That what you are trying to do is call a constructor without parameters, which is not defined in the code (that's why it marks an error in that line).

The solution would be to define a new constructor (there may be more than one) that does not have parameters and that uses a default array inside:

Vendedor::Vendedor()

Alternatively, do something like this: Vendedor ob = new Vendedor(arr); , having defined arr as a float array of size 12.

    
answered by 21.02.2016 / 05:42
source
2

As you have already answered Alvaro Montoro one of the problems you have is having created a constructor that receives a parameter, which prevents you from building a Vendedor without providing a parameter, that is totally true, so I am going to focus on answering your question about the " constructor starting each element of my array in 0.0 ".

As already mentioned, you do not have to create a constructor, you can initialize to 0 all the members in the creation place as you already do with Ventas::VentasTotal :

class Vendedor
{
public: 
    // Sin constructor
    void asignaventas(float);
    void imprimeventas();
private: 
    float Ventas[12] = {}; // Inicializa a 0 los 12 elementos
    float VentasTotal = .0f; 
    float CalVtasTotal(float);
    float Ventastotal = .0f;
};

Since Ventas[12] is an aggregate you can benefit from the aggregate initialization , which as you can See the link provided:

  

If the number of initializer clauses is less than the number of members or the initialization list is completely empty , the remaining members will be initialized with their initializers by default, if they have been provided in the definition of the class, or otherwise (from C ++ 14) by empty lists, which perform an initialization-value .

(I have modified the text because the automatic translation was strange , the highlighted in bold is mine)

In the case of float the initialization-value performs a initialization to zero :

  
  • For each variable called with duration of static or local storage of threads, before any other initialization.
  •   
  • As part of the initialization-value sequence of types other than classes and for members without a class constructor initialized by value .
  •   
  • When a character array is initialized with a literal string that is too short, the rest of the array is zero initializes.
  •   

    (I have modified the text because the automatic translation was strange , the highlighted in bold is mine)

    With the suggested changes you can use Vendedor in the following way:

    int main()
    {
        Vendedor v; // v.Ventas inicializado a 0, igual que el resto de miembros
        v.imprimeventas(); // Todo debe ser 0
        return 0;
    }
    

    It can be seen that we have not passed parameters to the constructor because the default constructor (which does not receive parameters) that the compiler itself has generated by us is used.

    As alternatives to float Ventas[12] = {}; we have:

    float Ventas[12]{};
    float Ventas[12] = { .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f };
    float Ventas[12]{ .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f, .0f };
    

    All of them have the same effect of setting all the values of the array to 0.

    You can see here an example of usage.

    PS:

    If we are initializing values float I prefer to use literal float ; a literal 0 is of type int , while a literal 0.0 is of type double .

        
    answered by 22.02.2016 в 13:16