0
votes

The errors are:

d_start is a protected member of CourseActivity
duration is a protected member of CourseActivity
location is a protected member of CourseActivity

class CourseActivity{

protected:
    StartTime* d_start;
    double duration;
    std::string location;

public:
    CourseActivity() = default;
    CourseActivity(const StartTime* _start, double _duration,
                   const std::string_location);
    void reschedule(StartTime* _newStart);
    void print() const;

}; 



class Lecture: public CourseActivity{
    std::string topic;
    bool deflt = false; //indicate which constructor was used.
                        //false = 1st. true = 2nd

public:
    Lecture(const StartTime* _start, double _duration,
            const std::string location, const std::string& _topic);
    Lecture(const CourseActivity& _oActivity, const std::string& topic );
    void print();
};

// ERROR
Lecture::Lecture(const CourseActivity& _oActivity, const std::string& _topic)
: CourseActivity(_oActivity.d_start,_oActivity.duration,_oActivity.location){
    topic = _topic;
    deflt = true;
}
// ERROR 
1
What causes the error? There's no code here that'd do that. - Alec Teal
You can only access the protected methods of the parent from an object that is descending from that parent. Here, oActivity is not a parent of the newly created object, hence its protected members cannot be accessed. - Ashalynd
Or maybe it's a duplicate of this question. Anyway, it's a duplicate. - dyp
Thank you it is a duplicate. I apologize - Hazem Beshara

1 Answers

1
votes

You are passing an instance of CourseActivity to the function Lecture::Lecture. Even while CourseActivity indeed is the base class of Lecture, you cannot access protected class members from the outside (like _oActivity.duration) even if the object you're operating on is of a derived type.

To avoid your particular problems, you may create this constructor in the base class

CourseActivity::CourseActivity(const CourseActivity &_oActivity)

and call it with

Lecture::Lecture(const CourseActivity& _oActivity, const std::string& _topic)
    : CourseActivity(_oActivity)

in the derived class. In the base class, you can then access the protected members, as opposed to in the derived class, where this is not allowed.