Hello all,
I'm trying to write an implementation of a 2-3-4 tree in c++. I'm it's been a while since I've used templates, and I'm getting some errors. Here's my extremely basic code framework:
node.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef TTFNODE_H
#define TTFNODE_H
template <class T>
class TreeNode
{
private:
TreeNode();
TreeNode(T item);
T data[3];
TreeNode<T>* child[4];
template <class U> friendclass TwoThreeFourTree;
int nodeType;
};
#endif
And when I try to compile from the command line with:
g++ main.cpp node.cpp TwoThreeFourTree.cpp
I get the following errors:
main.cpp: In function ‘int main()’:
main.cpp:14: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> class TwoThreeFourTree’
main.cpp:14: error: expected a type, got ‘TreeNode’
main.cpp:14: error: invalid type in declaration before ‘;’ token
My main question is why it's not seeing TreeNode as a type. Does anyone have any ideas? Thanks for all advice/help in advance...
Dan
and now it seems like it's having a problem with that. New errors:
main.cpp:(.text+0x255): undefined reference to `TwoThreeFourTree<TreeNode<int> >::TwoThreeFourTree()'
You need to move your template function bodies into the headers.
Also doing a Tree of a TreeNode doesn't really make sense now that I think about it. TwoThreeFourTree already has TreeNode<T> so by giving it TreeNode<int> you effectively have a TreeNode< TreeNode< int > >.
TwoThreeFourTree<int> makes a lot more sense. Assuming you want this tree to have ints.
I guess my question is, "How do I specify the type of a templated type?"
? You put it in <>'s. Or am I misunderstanding your question.
Your TwoThreeFourTree class already has Nodes in it. The T passed to the tree is the T used in the node. I gather that the 'T' is the type of data you want to store in the tree, right?
So if you want to have a tree of ints, you'd do TwoThreeFourTree<int>
The thing is I've got a ton of functions in TwoThreeFourTree and it seems like it might be messy to include them all in the header file...
An alternative approach:
change "TwoThreeFourTree.cpp" to "TwoThreeFourTree.hpp" and #include it at the end of TwoThreeFourTree.h