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 
dyp
  • 38,334
  • 13
  • 112
  • 177
  • possible duplicate of [access protected inherited member with pointer to base class](http://stackoverflow.com/questions/15969658/access-protected-inherited-member-with-pointer-to-base-class) – dyp Oct 14 '13 at 00:04
  • What causes the error? There's no code here that'd do that. – Alec Teal Oct 14 '13 at 00: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 Oct 14 '13 at 00:10
  • Or maybe it's a duplicate of [this question](http://stackoverflow.com/q/19328410/420683). Anyway, it's a duplicate. – dyp Oct 14 '13 at 00:12
  • Thank you it is a duplicate. I apologize – Hazem Beshara Oct 14 '13 at 00:15
  • @HazemBeshara Duplicates are sometimes hard to find. No problem. I was still looking for a better match, maybe [this one](http://stackoverflow.com/q/13723217/420683). – dyp Oct 14 '13 at 00:17
  • Is there another way to work around this besides using a friend class or changing the visibility of those attributes to public? – Hazem Beshara Oct 14 '13 at 00:21
  • @HazemBeshara Maybe delegate to `CourseActivity`'s copy-ctor? `: CourseActivity(_oActivity)` – dyp Oct 14 '13 at 00:22

1 Answers1

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.

Atle
  • 1,867
  • 12
  • 10
  • "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 Oct 14 '13 at 00: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 Oct 14 '13 at 00:22
  • The code might be in the class `CourseActivity` itself; in that case, it is allowed. [Live example](http://coliru.stacked-crooked.com/a/05073431636e9459) – dyp Oct 14 '13 at 00:23
  • True. Can't you (= asker) just pass the whole `_oActivity` into a constructor of `CourseActivity` ? – Atle Oct 14 '13 at 00: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 Oct 14 '13 at 00:33
  • I added this to my answer also. – Atle Oct 14 '13 at 00:55
  • "unless this is done inside a class of the same type" and even if the object you're operating on is of a derived type. And you don't need to create a copy-ctor, it is created automatically in this case (if you need another functionality then member-wise copy, then you'll have to implement it yourself of course). [Live example](http://coliru.stacked-crooked.com/a/fd9e6a2bb5af4801) – dyp Oct 14 '13 at 01:00
  • Edited again. Is the answer going to evolve any further now? – Atle Oct 14 '13 at 01:21
  • @Atle Ad `protected` and derived class objects: I just mentioned that as a curious fact ;) It's irrelevant here, but I might not have expressed myself clearly. Access to protected members is dependent on the type of the object which contains those members. You *can* access the protected members of objects of your own class and derived classes (even if those members have been declared in a base class of your own class), but not of any object of a base class. – dyp Oct 14 '13 at 11:53