2009-11-27 52 views
4

我想寫2-3-4樹的用C實現++。我已經使用模板已經有一段時間了,並且出現了一些錯誤。這是我非常基本的代碼框架:
node.h:C++模板友元類

#ifndef TTFNODE_H 
    #define TTFNODE_H 
    template <class T> 
    class TreeNode 
    { 
     private: 
     TreeNode(); 
     TreeNode(T item); 
     T data[3]; 
     TreeNode<T>* child[4]; 
     friend class TwoThreeFourTree<T>; 
     int nodeType; 
    }; 
    #endif 

node.cpp:

#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

#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:

#include "TwoThreeFourTree.h" 
#include <iostream> 
#include <string> 

using namespace std; 

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

而且main.cpp中:

#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(); 
} 

,當我嘗試用下面的命令行編譯: G ++ main.cpp中node.cpp TwoThreeFourTree.cpp

我收到以下錯誤:

In file included from TwoThreeFourTree.h:1, 
      from main.cpp:1: 
node.h:12: error: ‘TwoThreeFourTree’ is not a template 
main.cpp: In function ‘int main()’: 
main.cpp:13: error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> class TwoThreeFourTree’ 
main.cpp:13: error: expected a type, got ‘TreeNode’ 
main.cpp:13: error: invalid type in declaration before ‘;’ token 
In file included from node.cpp:1: 
node.h:12: error: ‘TwoThreeFourTree’ is not a template 
In file included from TwoThreeFourTree.h:1, 
      from TwoThreeFourTree.cpp:1: 
node.h:12: error: ‘TwoThreeFourTree’ is not a template 

我的主要問題是,爲什麼它說:「錯誤:‘TwoThreeFourTree’不是一個模板」。有沒有人有任何想法?感謝提前全部意見/幫助... 丹

+0

精確重複數據刪除 - 見http://stackoverflow.com/questions/206045/how-do - 你標記-A-結構模板-AS-朋友 – 2009-11-27 20:53:45

+0

不太一個「精確」的欺騙,現在是什麼呢? – danwoods 2009-11-27 21:33:49

+1

@Pavel:不是一個確切的重複,在這裏用戶不希望模板的所有實例來訪問,但具有相同特定類型的,而只有實例。一個'TwoThreeFourTree '應該有機會獲得'樹節點'但不一定'樹節點' – 2009-11-28 13:01:29

回答

5

你只需要在使用Friend關鍵字來聲明它作爲模板。您在代碼中使用不正確的語法來編寫朋友聲明。你想要寫的是:

template <class U> friend class TwoThreeFourTree; 
+0

謝謝Charles Salvia,那工作完美!現在要弄清楚,「main.cpp中:13:錯誤:在模板參數列表參數1型/價值不匹配‘模板類TwoThreeFourTree’的main.cpp:13:錯誤:預期的類型,有‘樹節點’.. 。 – danwoods 2009-11-27 21:40:02

+1

有原代碼的意圖,並在修改後的代碼語義之間的微小差異:在這裏您聲明TwoThreeFourTree'的'所有實例(與任何類型的U)作爲朋友,從而'TwoThreeFourTree '訪問'樹節點「 – 2009-11-28 12:36:39

7

已經接受了開放類的TwoThreeFourTree模板的任何實例的輕微問題的解決方案,不僅是那些共享相同的實例類型。

如果你只是想打開類,你可以使用下面的語法相同類型的實例:

template <typename T> class TwoThreeFourTree; // forward declare the other template 
template <typename T> 
class TreeNode { 
    friend class TwoThreeFourTree<T>; 
    // ... 
}; 
template <typename T> 
class TwoThreeFourTree { 
    // ... 
}; 
+0

謝謝,不知道我必須做一個向前聲明得到它的工作!謝謝!!! :) – leetNightshade 2012-03-03 00:01:10