All objects in my program inherit from a Container class. The Container class has a virtual BaseNode* getParent() const;
method and a virtual void setParent(BaseNode *p);
method.
I have a Set
class (Set in a tennis match, not a data structure) which has the Match
class as it's parent (via setParent()
) but since Set
inherits from Container
, The program creates a tree structure from the top down and the Set
class is a child, it doesn't need to have methods to track and maintain information about it's parent beyond what Container
provides.
The error C++: invalid conversion from ‘BaseNode*’ to ‘Match*’
shows up in the method below when I try to compile my program. (Player* getPlayer1() const;
only exists in the Match
class)
Player* Set::getPlayer1() const{
return getParent()->getPlayer1();
}
This is my inheritance structure for Match. (Note that TreeNode
is a template)
Match -> TreeNode<Set> -> BaseNode -> Container
I don't understand why I'm getting a conversation error. I have tried reading my textbook but it's a rather poor reference. Google just provided too much irrelevant information.
Edit
Player* Set::getPlayer1() const{
return dynamic_cast<Match>(getParent())->getPlayer1();
}
causes
error: cannot dynamic_cast ‘#‘obj_type_ref’ not supported by dump_expr#<expression error>((&((const Set*)this)->Set::<anonymous>))’ (of type ‘class BaseNode*’) to type ‘class Match’ (target is not pointer or reference)
Edit 2
I just realized I need dynamic_cast<Match*>
which works.