Constructor of a class requires initilization of hand made class

I have a class called ActivityManager and it has a field which is of type Activity[].The point is that I have made the class Activity.

#include <string>
#include "DateTimes.cpp"

using namespace std;

class Activity{

private:

    string activityName;
    DateTimes dateTimes;
    int importance;

public:

    Activity(string activityNamey,DateTimes dateTimesy,int importancey)
    {
        activityName = activityNamey;
        dateTimes = dateTimesy;
        importance = importancey;
    }
    ~Activity()
    {
    }
    string getActivityName()
    {
       return activityName;
    }
    DateTimes getDateTimes()
    {
        return dateTimes;
    }
    int getImportance()
    {
        return importance;
    }
};

.Now when I try to write the constructor for the class ActivityManager:

#include "Activity.cpp"
#include "DateTimes.cpp"


class ActivityManager
        {
        private:

            Activity activities[100]

        public:

            ActivityManager() {
            }
};

my editor CLion underscores the ActivityManager.When I go into the details by hovering the mouse over ActivityManager this message is printed out:

Constructor for 'ActivityManager' must explicitly initialize the member 'activities' which does not have a default constructor member is declared here.

Now there are some ways to fix up the problem , given out as recommendation from CLion such as Add constructor initializer for field activities.What does this do?And why is there a issue in the first place?I dont want to initialize anything I just want to use the array of type Activity[].I dont want to bind the Activity class to the ActivityManager I just want to use the array of type Activity[] as a field.What should I do?

  • 1

    Reads like you are looking for the Member Initializer List.

    – 

  • 3

    You’ll also run into problems with the activities array. Arrays are brutally simple. When you define an array you also have to initialize ALL of the elements in it, there are no “empty” slots in an array, and if the initialization is complicated, this is a serious drag. Can we talk you into using a std::vector instead?

    – 




  • 3

    #include "DateTimes.cpp" — Why are you including a .cpp file? A .cpp file suggests it is a standalone source file that needs to be compiled separately. What is done by convention is to include .h or .hpp files.

    – 




  • 4

    “im done with header files honestly” – What does that mean? It doesn’t become simpler just because you include a .cpp file.

    – 

  • 2

    “im done with header files honestly” Good, so you don’t plan to get employed as a programmer, right?

    – 

In your ActivityManager, the declaration Activity activities[100]; declares an array of 100 Activity instances. The array requires these instances to be initialised. Unfortunately, your Activity has no default constructor (i.e. a constructor without parameter). So your compiler doesn’t find a way to initialise the required activities.

The main issue here is that an array in C++ contains the number of objects foreseen in its maximal size. C++ is not like C where you could leave array element unitialized and initialise them later.

A better approach is therefore to replace the array with a std::vector<Activity> activities;. The big advantage is that the vector is really of variable size and only creates an Activity when you add one to the vector. And at this moment, you can very well use a constructor with parameters.

Leave a Comment