0

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 
8
  • possible duplicate of access protected inherited member with pointer to base class
    – dyp
    Commented Oct 14, 2013 at 0:04
  • What causes the error? There's no code here that'd do that.
    – Alec Teal
    Commented Oct 14, 2013 at 0:05
  • 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
    Commented Oct 14, 2013 at 0:10
  • Or maybe it's a duplicate of this question. Anyway, it's a duplicate.
    – dyp
    Commented Oct 14, 2013 at 0:12
  • Thank you it is a duplicate. I apologize Commented Oct 14, 2013 at 0:15

1 Answer 1

1

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.

9
  • "the instance you are passing in is not necessarily the same as the current base class" Are you referring to the reference const CourseActivity&? It's not even allowed when passing by value.
    – dyp
    Commented Oct 14, 2013 at 0:19
  • When you access _oActivity.duration, you are accessing the CourseActivity class from the outside, which means that protected or private members cannot be accessed. It does not matter where in the code this is, it might even be inside the same class.
    – Atle
    Commented Oct 14, 2013 at 0:22
  • The code might be in the class CourseActivity itself; in that case, it is allowed. Live example
    – dyp
    Commented Oct 14, 2013 at 0:23
  • True. Can't you (= asker) just pass the whole _oActivity into a constructor of CourseActivity ?
    – Atle
    Commented Oct 14, 2013 at 0:31
  • Yes, I also suggested this in a comment to the OP. After thinking a while about that, I'd even recommend doing that. The base class is responsible for the copy, that shouldn't be the job of the derived class.
    – dyp
    Commented Oct 14, 2013 at 0:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.