Question

I'm currently learning C++ and reading through "C++ Primer 5th Edition". I just started learning about constructors and I'm having a bit of a problem that I can't figure out.

#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <string>
struct Sales_data
{
    //default constructor
    Sales_data(const std::string &s, unsigned n, double p):
               bookNo(s), units_sold(n), revenue(p*n) { } 
    //new members: operations on Sales_data objects
    std::string isbn() const { return bookNo; }
    Sales_data& combine(const Sales_data&);
    double avg_price() const;
    //data members
    std::string bookNo;
    unsigned units_sold;
    double revenue;
};

I'm pretty sure that the default constructor I wrote is correct (considering it's the one written in the book), but obviously I'm missing something here. I don't see any syntax errors or anything and all of the built-in members are being initialized so I have no idea what's wrong.

EDIT :

I just found out that it's not my header file giving the error, it's actually my source file. When I create a Sales_data object like:

Sales_data total;

it gives me the "No appropriate default constructor available" error. I'm still unsure as to what's wrong considering the author gave three ways to write a default constructor, these are them:

struct Sales_data {
// constructors added
Sales_data() = default;   //Number 1
Sales_data(const std::string &s): bookNo(s) { }    //Number 2
Sales_data(const std::string &s, unsigned n, double p):    //Number 3
           bookNo(s), units_sold(n), revenue(p*n) { }

If those aren't default constructors, then what exactly are they/is their purpose?

Was it helpful?

Solution

A default constructor is a constructor that can be called without passing any argument.

This means that it must take no parameters or that all of them must have a default value.

The default constructor is needed for example when you write

MyClass x;

because the compiler must be able to generate code to build such an object without any arguments.

Also standard containers may require a default constructor depending on how you use them: for example if you use std::vector::resize the library can be asked to increase the size of a vector containing your class instances, thus it must be able to create elements without providing any argument.

OTHER TIPS

The problem isn't related to default constructors. The problem indeed is in Sales_data total;. What's the book number, sales price and number sold for total? You must provide them when constructing total.

The constructor of your class takes three parameters, those must be provided when you try to construct an object. Your current variable declaration doesn't provide any parameters:

Sales_data total;

Because there are no parameters provided, the compiler tries to use a constructor that doesn't take any parameters. Such a constructor is also called "default constructor". Your class doesn't have a constructor without parameters, so this doesn't work.

To use the existing constructor, you have to provide the parameters when you create the object:

Sales_data total("books", 28, 15.99);

Alternatively you could add a constructor to Sales_data that doesn't take any parameters (a default constructor), and initializes the class with some default values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top