specifying templated class types

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> friend class TwoThreeFourTree;
          int nodeType;  
        };  
        #endif 


node.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    #include "node.h"

    using namespace std;
    template <class T>
    //default constructor
    TreeNode<T>::TreeNode(){
    }

    template <class T>
    //paramerter receving constructor
    TreeNode<T>::TreeNode(T item){
    data[0] = item;
    nodeType = 2;
    }

TwoThreeFourTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
    #include "node.h"
    #ifndef TWO_H
    #define TWO_H
    enum result {same, leaf,lchild,lmchild,rmchild, rchild};
    template <class T> class TwoThreeFourTree
    {
      public:
        TwoThreeFourTree();
   
      private:
        TreeNode<T> * root;
    };
    #endif 

TwoThreeFourTree.cpp:
1
2
3
4
5
6
7
8
9
10
    #include "TwoThreeFourTree.h"
    #include <iostream>
    #include <string>

    using namespace std;

    template <class T>
    TwoThreeFourTree<T>::TwoThreeFourTree(){
      root = NULL;
    }

And main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    #include "TwoThreeFourTree.h"
    #include <string>
    #include <iostream>
    #include <fstream>

    using namespace std;

    int main(){
      ifstream inFile;
      string filename = "numbers.txt";
      inFile.open (filename.c_str());
      int curInt = 0;
      TwoThreeFourTree <TreeNode> Tree;

      while(!inFile.eof()){
        inFile >> curInt;
        cout << curInt << " " << endl;
      }

      inFile.close();
    }

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

TreeNode isn't a type, it's a template.

TreeNode<int> is a type, or TreeNode<float>, or whatever.

Also you'll run into linker errors. Typically template classes need to be fully defined in the header file. (There are ways around it but meh)
Changed main.cpp to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "TwoThreeFourTree.h"
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main(){
  ifstream inFile;
  string filename = "numbers.txt";
  inFile.open (filename.c_str());
  int curInt = 0;
  //template <class T>
  TwoThreeFourTree < TreeNode<int> > Tree;

  while(!inFile.eof()){
    inFile >> curInt;
    cout << curInt << " " << endl;
  }

  inFile.close();
}


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()'

collect2: ld returned 1 exit status
That's the linker errors I was talking about.

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?"
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...
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
Topic archived. No new replies allowed.