一個基本的二叉樹我的頭文件包括以下內容:返回一個指向類定義的結構,多範圍經營
template<typename T>
class Btree{
// OVERVIEW: a binary tree with flexible structure that is not sorted
private:
struct node { //a container object
node *left; //left and right tree
node *right;
T *o; // pointer to object of node
};
public:
node *root; //pointer to the root of the tree (NULL if empty)
node* insert (node *parent, T *child, int child);
//MODIFIES: this
//EFFECTS: creates a node that stores a pointer to the new child
// and returns the pointer to the node of the new child
// the integer child is either 0, for left child,
// or anything else for right child
// void printTree (node * root);
//EFFECTS: takes the root of a tree and prints the tree's
// coordinates
Btree(){}; //ctor
Btree(){} //dtor
};
#include "btree.cpp"
我的.cpp看起來是這樣的,並注意它是包含在我的頭的底部,以避免模板編譯器錯誤:
template <typename T>
Btree<T> :: node * Btree<T>::insert (node *parent, T *child, int child)
{
node *np = new node;
np-> o = child;
np->left = NULL;
np->right = NULL;
if (child == 0)
parent->left = np;
else
parent->right = np;
return np;
}
然而
,我得到以下編譯器錯誤:
btree.cpp:3: error: expected constructor, destructor, or type conversion before ‘*’ token
我在用g ++編譯版本4.1.2。
任何人都可以幫忙嗎?
這是C++,對不對?你能用這個來重新嗎?你必須擺脫你的一個標籤,但你的問題會被更合適的觀衆看到。 –
*我的.cpp看起來像這樣,而不是它包含在我的標題底部以避免模板編譯器錯誤* Ehm是什麼? – FailedDev
請不要再添加這些標籤!他們不會幫助。 C++就夠了。 – Beginner