I want to implement a generic, tree structure with C++ - with classes! - and this tree is composed by a key (which in my case is an integer) and a leftChild and rightChild attributes, which should be both of the same type as the tree itself
In C I can do this like:
typedef struct avl {
int key;
int bf;
struct avl *leftChild;
struct avl *rightChild;
} AVLTree;
And I attempted the following in my C++ code:
class MTree {
public:
int key;
int bf;
MTree leftChild;
MTree rightChild;
MTree() {}
~MTree() {};
}
But it won't work and it gives me the following error message:
mtree-ops.cpp:12: error: field ‘leftChild’ has incomplete type
mtree-ops.cpp:13: error:error: field ‘rightChild’ has incomplete type
So you see, looks like I can't say my class has an attribute of its own type because that's like trying to make reference to something that doesn't really exist at the time of the definition. How can this be done using C++ classes?