This is the definition of Parent:
1 2 3
|
class Parent{
virtual void print()=0;
};
|
Parent does not have a member called "contain". It only has print(), as a pure virtual function.
I don't know exactly what your design constraints are, but if your Child class has a particular way of sorting, then make a function that accepts a Child object. And if you want this function to be able to directly access your contain vector, then you need to provide a getter, make it public, or make sort a friend. Again, it really depends on what your design requirements are, or what kinda of flexibility you want to provide.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <vector>
using std::vector;
class Parent {
virtual void print()=0;
};
class Child;
void sort(Child& child);
class Anon;
class Child : public Parent {
private:
vector<Anon*> contain;
public:
void print(){
//print contain
}
void organize(){
sort(*this);
}
friend void sort(Child& child);
};
class Anon {
};
void sort(Child& child) {
for (Anon* element : child.contain)
{
// ...
}
}
int main()
{
Child child;
child.organize();
}
|
If you need the sort function to take in a Parent pointer, then you should put the common data in the base class. e.g. put the vector in the base class, or make a "getContainer" virtual function.
If you told us what the actual purpose of this is, maybe someone could help come up with a design that doesn't seem contrived.