Reputation: 269
Pretty new to C++ and having my first try at templates. I created a struct, treeNode
, having left, right and parent pointers. I want the tree to be able to store multiple data types and hence am using templates. Whenever I try create an instance of the struct in the .cpp file, and then use it to access the left/right pointers of the tree I get this error: Member access into incomplete type struct 'treeNode'. Any idea on what I'm missing?
Here is the code in the .h file(struct definition):
template <class T>
struct treeNode {
node<T> *l;
node<T> *r;
node<T> *p;
};
Here is my attempt in the .cpp file:
#include "RedBlack.h"
struct treeNode* t;
Tree::Tree() {
t->l = NULL;
}
Upvotes: 0
Views: 2867
Reputation: 133679
struct
, members should have the same type of the struct
itself.Tree
class below)Since you want to have a generic tree, then the Tree
class should be generic too:
template <class T>
struct node {
node<T> *l;
node<T> *r;
node<T> *p;
};
template <class T>
class Tree
{
private:
node<T> *root;
public:
Tree() : root(nullptr) { }
};
Tree<int> *tree = new Tree<int>();
Upvotes: 2
Reputation: 311186
Apart from that you did not set a template argument it seems you made a typo. Instead of
template <class T>
struct treeNode {
node<T> *l;
node<T> *r;
node<T> *p;
};
should be
template <class T>
struct treeNode {
treeNode<T> *l;
treeNodeT> *r;
treeNode<T> *p;
};
Upvotes: 0
Reputation: 101506
A treeNode
is nothing -- literally not a thing -- without it's template parameters.
You must do:
treeNode <int> * t;
Or something similar.
Upvotes: 0