[Solved] Error In classes C++


If you do not provide any constructor in your class, the compiler will automatically create one for you.
In your class, you have specified a particular constructor:

MonthData(double, int, double, double, int, double, double);

As soon as you provide any constructor, the compiler will not create a default constructor (i.e. one that takes no parameters).

You are calling

MonthData data();

You are passing no parameters here but you have no constructor that takes no parameters.
You probably meant to call

MonthData data(with 7 parameters);

Alternatively, add the following to your MonthData class body:

MonthData();

Then in your data.cpp, need to provide the code for what this constructor should do, i.e.

MonthData::MonthData()
{
   //Initialise as required - but better to use a member initialization list
}

It would be better to use a member initialization list for your 7 member variables.
For example:

MonthData::MonthData()
 : year(2014), month(2), temp_maximum(15.4), temp_minimum(2.1), air_frost(5), rain(5.6), sun(7.6)    //Use desired default values
{}

This simple constructor code could instead be put directly in the class body in the header file:

class MonthData
{
  public:
    //overload constructor
    MonthData(double y, int m, double max, double min, int fr, double r, double s)
: year(y), month(m), temp_maximum(max), temp_minimum(min), air_frost(fr), rain(r), sun(s) {};

    //default constructor
    MonthData()
: year(2014), month(2), temp_maximum(15.4), temp_minimum(2.1), air_frost(5), rain(5.6), sun(7.6) {};

    etc
};

In addition – review your variable types. Should year really be a double? Also you have month declared as a double but your accessor returns an int.

solved Error In classes C++