0
votes

I have a base class and a class derived from it. In the example below, Frame_Pic is a derived class of Base_Pic, the details of which I have not included. Frame_Pic has members, vertical,horizontal, and corners which is not in the base class. Picture is an interface class and basically holds a pointer to Base_Pic.

When I try to assign new values to these in the reframe function below, I get an error stating that Base_Pic has no such data members.

The action, pic.p->vertical, will think I am accessing the base class. If I put these data members in the base class, the rest of my functions are written in a way that it will not accommodate this addition. How can I assign to these data members?

Picture frame(const Picture& pic, char v, char c, char h)
{
    Frame_Pic* temp = new Frame_Pic(pic.p);
    temp->vertical = v;
    temp->horizontal = h;
    temp->corners = c;
    Picture temp2(temp);
    return temp2;
}

void reframe(Picture& pic, char v, char c, char h)
{
    pic.p->vertical = v;
    pic.p->horizontal = h;
    pic.p->corners = c;
}
3
Is Picture your Base_Pic or Frame_Pic class?FuzzyBunnySlippers
Can you post the actual error message? And show us how you initialize the objects that you send to the function.0x499602D2
Are you, perhaps, trying to access members in Frame_Pic through a pointer (or reference) to Base_Pic?FuzzyBunnySlippers
@NonlinearIdeas - see this line in the question - "Picture is an interface class and basically holds a pointer to Base_Pic."user2428400

3 Answers

0
votes

Instead of accessing them directly, use your Picture interface to declare set and get methods (assuming both are needed) for those members, and implement them in the derived class.

Direct access to member variables does not behave polymorphically, hence your error.

0
votes

Sounds like you want to set items of a derived class while feeding a function a pointer to the base type. The way to do this is to create a virtual function in the base class and implement it in the derived class.

for instance...

virtual void BaseClass::SetupSomeStuff(...) {
}

void DerivedClass::SetupSomeStuff(...) {
your assignments here
}
0
votes

You should rethink the way you wrote the functions and how the classes interact in general.
Reframe might be a function member of Frame_Pic but its functionality has nothing to do with any instance of the class Frame_Pic. In fact, since all you can know for sure is that Picture holds a pointer to a Base_pic, it might as well hold a pointer to an instance of a different class that derives Base_pic that does not define members named vertical, horizontal or corners.

Frame_picPicturestatic