我想寫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’不是一個模板」。有沒有人有任何想法?感謝提前全部意見/幫助... 丹
精確重複數據刪除 - 見http://stackoverflow.com/questions/206045/how-do - 你標記-A-結構模板-AS-朋友 – 2009-11-27 20:53:45
不太一個「精確」的欺騙,現在是什麼呢? – danwoods 2009-11-27 21:33:49
@Pavel:不是一個確切的重複,在這裏用戶不希望模板的所有實例來訪問,但具有相同特定類型的,而只有實例。一個'TwoThreeFourTree'應該有機會獲得'樹節點'但不一定'樹節點' –
2009-11-28 13:01:29