Okay, so I am having some troubles in creating a Parent/Child relationship. The easiest way I can explain this is having an object, with a reference (or pointer) to another object of the same type, and then an array of children references (or pointers) to more objects. The object should have functions like .getChildren, .addChild, .removeChild, .getParent, .changeParent. I'm having a horrible time with the pointers, and if anyone could help with the code that'd be great. Also, in case anyone is curious, I'm going to use this approach with 3D models. The base model (parent) will be the center of the object, all children can move freely, and when the parents moves, it causes the children to move.
Code:
class Base {
protected:
Base* parent;
std::vector<Base*> children;
std::string id;
POINT pos, rot;
public:
Base (void);
Base (std::string);
Base (POINT, POINT, std::string);
Base (const Base&);
~Base (void);
POINT getPos (void);
POINT getRot (void);
Base getParent (void);
Base getChildren (void);
void addChild (Base&);
void removeChild (Base&);
void changeParent (Base);
void move (int, int);
void rotate (int, int);
void collide (Base);
void render (void);
};
Base::Base (void) {
this->id = getRandomId();
this->pos.x = 0; this->pos.y = 0; this->pos.z = 0;
this->rot.x = 0; this->rot.y = 0; this->rot.z = 0;
};
Base::Base (std::string str) {
this->id = str;
this->pos.x = 0; this->pos.y = 0; this->pos.z = 0;
this->rot.x = 0; this->rot.y = 0; this->rot.z = 0;
};
Base::Base (POINT p, POINT r, std::string str) {
this->id = str;
this->pos = p;
this->rot = r;
};
Base::Base (const Base& tocopy) {
this->parent = tocopy.parent;
this->children = tocopy.children;
this->id = tocopy.id;
this->pos = tocopy.pos;
this->rot = tocopy.rot;
};
Base::~Base (void) {
};
void Base::changeParent (Base child) {
*(this->parent) = child;
};
int main (void) {
POINT p;
p.x=0;p.y=0;p.z=3;
Base A;
Base B(p, p, "Unique");
printf("A.pos.z is %d and B.pos.z is %d\n", A.getPos().z, B.getPos().z);
B.changeParent(A);
printf("B.parent.pos.z %d should equal 0\n", B.parent->getPos().z);
The error I get with the code is: error C2248: 'Base::parent' : cannot access protected member declared in class 'Base' Also, if I make everything public, it'll compile fine, but then it crashes on run.
Note: I didn't copy all of the code, just what I thought would be relevant.
EDIT: Full dump of the error:
(152) : error C2248: 'Base::parent' : cannot access protected member declared in class 'Base'
(20) : see declaration of 'Base::parent'
(18) : see declaration of 'Base'