Creating a Parent/Child relationship with objects
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: 'Bas开发者_开发知识库e::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'
This
printf("B.parent.pos.z %d should equal 0\n", B.parent->getPos()
raises your error because of this
protected:
Base* parent;
You're trying to reference B.parent
from outside of the class implementation. Because parent
is declared protected
it isn't available there. In your declaration of Base
, you should add an accessor function that returns parent and is public, like:
public:
inline Base* getParent() { return parent; }
It always helps if you tell us which line has the error. If you'd mentioned it was this line, you'd have 10 answers by now.
printf("B.parent.pos.z %d should equal 0\n", B.parent->getPos().z);
int main()
cannot access protected members of Base
, and thus cannot access B.parent
. Replace this with:
printf("B.getParent().pos.z %d should equal 0\n", B.getParent()->getPos().z);
http://ideone.com/CobdS Also note that getParent()
should probably return a pointer (Base*
), and getChildren()
should probably return const std::vector<Base*>&
精彩评论