2014-02-22 32 views
0
I am writing a binary search tree program and I am getting the error 
"Main.cpp:30:20: error: use of undeclared identifier 'T' 
BiTreeNode<T> *node = new BiTreeNode<T> (b);. 

我寫了2個其他程序使用類模板,我從來沒有得到這個錯誤之前。 任何幫助將不勝感激。提前致謝!我剛剛從Eclipse 切換到NetBeans。我不認爲這會導致錯誤,但我可能是錯了.... BiTreeNode.h的我不知道爲什麼我得到錯誤:「Main.cpp:30:20:錯誤:使用未聲明的標識符'T'」

//////Beginning of BiTree.h////// 

#ifndef BITREE_H 
#define BITREE_H 
#include "BiTreeNode.h" 
#include <iostream> 
using namespace std; 

template <class T> 
class BiTree 
{ 
    private: 
     BiTreeNode<T> *root; 

     //use const because when we insert the data of type T from the object, 
     //we want to only read it and not change it 
     void insert(const T &d, BiTreeNode<T> *ptr) 
     { 
      if(ptr == NULL) 
      { 
       //creates a new tree node with data type d and its right and 
       //left child node pointers NULL 
       ptr = new BiTreeNode<T>(d, NULL, NULL); 
      } 

      else if(d <= ptr -> data) 
      { 
       insert(d, ptr -> leftChild); 
      } 

      else if(d >= ptr -> data) 
      { 
       insert(d, ptr -> rightChild); 
      } 
     } 



    public: 

     BiTree() 
     { 
     root = NULL; 
     } 

     //By adding the &, the d copies the data from the object passed as an 
     //argument for the insert function in main into a new node which is then put into the tree 
     //Added the const since when we take in the data in from the object passed as an object 
     //for the insert function in main. We don't want to change the data, we just want to read it 
     void insert(const T &d) 
     { 
      //d is the data type you want to send in 
      insert(d, root); 
     } 

     //prints a tree rooted at ptr in sorted order 
     void inOrder(BiTreeNode<T> *ptr, ostream & out) 
     { 
      if(ptr != NULL) 
      { 
       inOrder(ptr -> leftChild, out); 
       out << ptr -> data << endl; 
       inOrder(ptr -> rightChild, out); 
      } 
     } 

     void preOrder() 
     { 
      cout << "printing out in pre-order" << endl; 
     } 

     void postOrder() 
     { 
     cout << "printing out in post-order" << endl; 
     } 

     void printTree() 
     { 

     } 


}; 

#endif /* BITREE_H */ 

//////End of BiTree.h////// 

//////年初///////

#ifndef BITREENODE_H 
#define BITREENODE_H 
#include "BiTree.h" 
#include <iostream> 
using namespace std; 
template <class T> 
class BiTree; 


template <class T> 
class BiTreeNode 
{ 
    private: 
     T data; 
     BiTreeNode<T> *leftChild; 
     BiTreeNode<T> *rightChild; 

    public: 
     BiTreeNode(T Data) 
     { 
      data = Data; 
      leftChild = NULL; 
      rightChild = NULL; 

     } 

     friend class BiTree<T>; 
}; 

#endif /* BITREENODE_H */ 

////////End of BiTreeNode.h////// 



////Start of Main.cpp///// 


#include <cstdlib> 
#include <iostream> 
#include <vector> 
#include <ctime> 
#include "BiTree.h" 
#include "BiTreeNode.h" 
using namespace std; 

int main() 
{ 
    srand(time(NULL)); 
    BiTree<int> tree; 


    int a = 1 + rand() % 100; 

    for(int i=0; i<a; i++) 
    { 
     int b = 1 + rand() % 100; 
     BiTreeNode<T> *node = new BiTreeNode<T>(b); 
    } 

    tree.postOrder(); 

    tree.preOrder(); 

    return 0; 
} 
/////End of Main.cpp////// 

回答

0

您需要在某處定義T。無論是包括你的Th文件或聲明的實際類T.

當我看到你定義了T A INT,則必須在該行替代T.通過INT如下:

BiTreeNode<int> *node = new BiTreeNode<int>(b); 
+0

非常感謝你很多,修復它! – Jok3r

+0

高興地幫助你。因此,您應該通過檢查來接受我的答案。你可能會另起爐竈。提前致謝。乾杯。 –

相關問題