I am having a hard time finding a simple solution to this. I am implementing an expression tree using the following classes, I declare a friend function of class Tree. My problem comes when I try to get it started in main.
template<class object> class Tree;
template<class object> Tree<object>::expTree(Tree<object> *&T); // ERROR
template<class object>
struct Node
{
object info;
Node *next;
Node<object>():info(0), next(NULL) {}
Node<object>(const object &element, Node *n = NULL):
info(element), next(n){}
};
template<class object>
class Stack
{
public:
Stack();
~Stack();
void makestackempty();
bool stackEmpty() const;
void push(object &item);
void pop (object &item);
void printStack() const;
private:
Node<object> *top;
};
template<class object>
struct TreeNode
{
object info;
TreeNode *right;
TreeNode *left;
TreeNode()
{}
};
template<class object>
class Tree
{
private:
TreeNode<object> *root;
public:
Tree();
~Tree();
Tree(const Tree<object> &rhs); // copy
void operator=(const Tree<object> &rhs);
void copyconst(TreeNode<object> *orig, TreeNode<object> *&rhs);
void makeEmpty(TreeNode<object> *&tree);
bool isEmpty()const;
friend Tree<object> expTree(Tree<object> *&T){
buildTree(T.root);
};
void buildTree(TreeNode<object> *&tree);
void printTree(TreeNode<object> *&tree)const;
};
In main, I get “error: expTree was not declared in this scope.”
I also get, ” error: expected constructor, destructor, or type conversion before â;â token”
on the second line of this code..
Anyone have any pointers?
This function template is actually a free function, and will be a friend of the class
Tree. So it should be written as:That is, remove
Tree<object>::from the declaration. I just realized that it is missing the return type, and I suppose you meanTree<object>to be the return type (andTree<object>::is a typo in your code). If so, then write this:I would like to comment on your style of naming the template parameters and arguments. It is customary to use
T,U,Vetc for template parameters, not for function argument. So it feels good when you write the declaration as:Well I just swapped the names.