My header file for a basic binary tree includes the following:
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"
My .cpp looks like this, and note that it is included at the bottom of my header to avoid template compiler errors:
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;
}
however, I get the following compiler error:
btree.cpp:3: error: expected constructor, destructor, or type conversion before ‘*’ token
I’m compiling with g++, version 4.1.2.
Can anyone help?
First of all, your destructor should have a
~in front of it, so changeto
Second, you need
typenamebefore the return type ofinsertbecause it is a dependent type:Also you need to rename one of your arguments, you have two named
child.