0

I'm receiving a common error with one of my classes, that of no default constructing being located for a specific class

Heres the class in question and its constructor

class Campaign {

public:
double funding;
double managerEffectiveness;
Date Day; //determine when they cant spend money anymore

Campaign(double funding, double managerEffectiveness, Date Day) {

funding = funding;
managerEffectiveness = managerEffectiveness;
Day =  Day;

}
};

The error is occurring at line 8, specifically Date Day. This variable of type Date can be used in a similar way in other constructors successfully, for example here is another class that utilizes the Date class. The exact error is no default constructor exists for class "Date"

class electrorateSupport: public Electrorates{

public:
Date Day;
double funding;
double financialEffectiveness();
double fudningImpact();


electrorateSupport(Date Day, double funding,double stanceDistriubtion, int Cluster)
:Electrorates(stanceDistriubtion,Cluster), Day(Day), funding(funding) {

Day = Day;
funding = funding;

}
);

This class is similar to the Campaign class, with the only big difference being it is a child class that inherits from another. I've been comparing these two classes to find any other differences which may cause my error but I can not be able to pinpoint any. I have also attempted to rewrite the class multiple times, still resulting in no definite issue. Thank you for any help!

EDIT date class has been provided

class Date {
     public:
     int Day;
     int Month;
     int Year;

     Date(int day, int month, int year) {
            this->Day = day;
            this->Month = month;
            this->Year = year;
        }

     void setDay(int day)
        {
            Day = day;
        }

        void setMonth(int month)
        {
          Month = month;
        }

       void setYear(int year)
        {
           Year = year;
        }

            int getDay()
        {
            return Day;
        }

        int getMonth()
        {
            return Month;
        }

        int getYear()
        {
            return Year;
        }

};
thealchemist
  • 421
  • 4
  • 12
Bao Ling
  • 69
  • 7
  • What does `Date` look like? –  May 17 '20 at 03:33
  • What is `Date`? – PaulMcKenzie May 17 '20 at 03:34
  • 1
    "`Day = Day`" -- what exactly do you expect to accomplish by assigning an object to itself? Is this what you expect to happen here? If not, you're surely mistaken, because that's what ***will*** happen here. Seems like you need to learn how to use the initialization section of the constructor. – Sam Varshavchik May 17 '20 at 03:34
  • 2
    `electrorateSupport`'s constructor initializes `Day` in its constructor initializer list. `Campaign`'s constructor does not, which means it needs to be default-initialized, but it apparently lacks default constructor. – Igor Tandetnik May 17 '20 at 03:37
  • @Chipster I have provided it – Bao Ling May 17 '20 at 03:38
  • @PaulMcKenzie I have provided it – Bao Ling May 17 '20 at 03:38
  • 1
    You need to understand more about constructors, read this https://www.geeksforgeeks.org/constructors-c/ – Jimish Fotariya May 17 '20 at 03:39
  • @IgorTandetnik thankyou for clearing that up its working now – Bao Ling May 17 '20 at 03:42
  • @JimishFotariya -- From that site: `Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters`-- The information at that site you linked to is incomplete or is misleading, as a lot of their information seems to be. A default constructor *can* have arguments, it is just that all the arguments must have default values. For example, `Day(int x = 0, int y = 0, int z=0);` is a default constructor. – PaulMcKenzie May 17 '20 at 04:06
  • @paulmckenzie, Technically default contructor doesn't "take" any arguments. But right, it can have default params. More constructtive defination is described at IBM knowledge center as `if a constructor has any arguments that do not have default values, it is not a default constructor` https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzarg/cplr376.htm – Jimish Fotariya May 19 '20 at 18:38

2 Answers2

0

the problem is not with the Date class,

what a default constructor (the error message) is a constructor with no parameters, so you need to have a constructor with no parameters even if it doesn't have a declaration.

This way:

Campaign() {}

that should solve your problem.

Note: error locations sometimes are confusing, don't ask me why.

Midoen
  • 46
  • 1
  • 4
0

Once you provide a constructor with any kind of parameters, the compiler will no longer provide you with a default constructor without asking it to.

You may still get your default constructor provided by the compiler by

Campaign()=default;

This, along with the other special member functions may be explicitly defaulted or deleted to force the compiler to generate one for you.

You can also just write an empty default constructor as well with

Campaign(){}

With only slight changes to the semantics

Colin Hicks
  • 348
  • 1
  • 3
  • 13