-
templates with pointers
i'm trying to construct binary trees and BST's with templates.
is that a bad idea? I'm getting template erros when compiling
and i can not figure it out. please let me know what is wrong with
the syntex. I attached my binaryTree.cpp , thank you
here's a sample :
template <class Type>
void BinaryTree<Type>::inorder(void (*visit)(Type)){
if (root!=0)inorder(root, visit);
}
template <TreeNode*t, class Type>
void BinaryTree<TreeNode*t, Type>::inorder(TreeNode *t, void(*visit)(Type)){
if (t->left!=0)inorder(t->left, visit);
visit(t->left);
if(t->right!=0)inorder(t->right, visit);
}
and i'm getting an overload function error which i don't understand because the do not accept the same amount of parameters.
-
Did you try this?
Code:
template <class Type>
class BinaryTree {
public:
void inorder(void (*visit)(Type)){
if(root!=0) {
inorder(root, visit);
}
}
void inorder(TreeNode *t, void(*visit)(Type)){
if(t->left!=0) {
inorder(t->left, visit);
}
visit(t->left);
if(t->right!=0) {
inorder(t->right, visit);
}
}
...or am I missing something?
-
????
yea, that was what i had before. this is my whole .cpp .
i get a few errors with this:
\BinaryTree.cpp(32) : error C2955: 'TreeNode' : use of class template requires template argument list
\treenode.h(30) : see declaration of 'TreeNode'
\BinaryTree.cpp(33) : error C2955: 'BinaryTree' : use of class template requires template argument list
\binarytree.h(33) : see declaration of 'BinaryTree'
\BinaryTree.cpp(33) : error C2955: 'TreeNode' : use of class template requires template argument list
\treenode.h(30) : see declaration of 'TreeNode'
\BinaryTree.cpp(37) : error C2244: 'BinaryTree::inorder' : unable to resolve function overload
\BinaryTree.cpp(39) : error C2954: template definitions cannot nest
\BinaryTree.cpp(44) : error C2955: 'TreeNode' : use of class template requires template argument list
\treenode.h(30) : see declaration of 'TreeNode'
\BinaryTree.cpp(56) : error C2955: 'TreeNode' : use of class template requires template argument list
\treenode.h(30) : see declaration of 'TreeNode'
\BinaryTree.cpp(60) : error C2244: 'BinaryTree<Type>::postorder' : unable to resolve function overload
---------------------------------------------------------------------------
i don't understand the function overload because my calling functions don't have the same parameters
and i don't understandthe "template requires template argument list"
those are my repeated erros with my binaryTree and BST class. let me know if you know what i'm doing wrong, thanks a lot
----------------------------------------------------------------------------------
#include <iostream.h>
#include "BinaryTree.h"
template <class Type>
BinaryTree<Type>::BinaryTree(){
root = 0;
}
template <class Type>
BinaryTree<Type>::BinaryTree(Type v, BinaryTree &l, BinaryTree &r){
root = new TreeNode(v,l.root,r.root);
}
template <class Type>
BinaryTree<Type>::~BinaryTree(){
if (root!=0)delete root;
}
template <class Type>
void BinaryTree<Type>::inorder(void (*visit)(Type)){
if (root!=0)inorder(root, visit);
}
// IT COMPILES TILL HERE ********************
template <TreeNode*t, class Type>
void BinaryTree::inorder(TreeNode *t, void(*visit)(Type)){
if (t->left!=0)inorder(t->left, visit);
visit(t->left);
if(t->right!=0)inorder(t->right, visit);
}
template <class Type>
void BinaryTree<Type>::preorder(void (*visit)(Type)){
if (root!=0)preorder(root, visit);
}
template <class Type>
void BinaryTree<Type>::preorder(TreeNode *t, void(*visit)(Type)){
visit(t->left);
if (t->left!=0)preorder(t->left, visit);
if(t->right!=0)preorder(t->right, visit);
}
template <class Type>
void BinaryTree<Type>::postorder(void (*visit)(Type)){
if (root!=0)postorder(root, visit);
}
template <class Type>
void BinaryTree<Type>::postorder(TreeNode *t, void(*visit)(Type)){
if (t->left!=0)preorder(t->left, visit);
if(t->right!=0)preorder(t->right, visit);
visit(t->left);
}
---------------------------------------------------------------------------------
#ifndef BinaryTree_H
#define BinaryTree_H
#include "Contact.h"
#include "TreeNode.h"
template <class Type>
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
BinaryTree(Type v, BinaryTree &l, BinaryTree &r);
void inorder(void (*visit)(Type));
void preorder(void (*visit)(Type));
void postorder(void (*visit)(Type));
protected:
TreeNode *root;
private:
void inorder(TreeNode *t, void (*visit)(Type));
void preorder(TreeNode *t, void (*visit)(Type));
};
#endif
-
Mostly just syntax errors, and some of the definitions were undeclared:
Code:
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
BinaryTree(Type v, BinaryTree &l, BinaryTree &r);
void inorder(void (*visit)(Type));
void preorder(void (*visit)(Type));
void reorder(void (*visit)(Type));
void postorder(void (*visit)(Type));
protected:
TreeNode *root;
private:
void inorder(TreeNode *t, void (*visit)(Type));
void preorder(TreeNode *t, void (*visit)(Type));
void reorder(TreeNode *t, void(*visit)(Type));
void postorder(TreeNode *t, void(*visit)(Type));
};
template <class Type>
BinaryTree<Type>::BinaryTree(){
root = 0;
}
template <class Type>
BinaryTree<Type>::BinaryTree(Type v, BinaryTree &l, BinaryTree &r){
root = new TreeNode(v,l.root,r.root);
}
template <class Type>
BinaryTree<Type>::~BinaryTree(){
if (root!=0)delete root;
}
template <class Type>
void BinaryTree<Type>::inorder(void (*visit)(Type)){
if (root!=0)inorder(root, visit);
}
template <class Type>
void BinaryTree<Type>::inorder(TreeNode *t, void(*visit)(Type)){
if (t->left!=0)inorder(t->left, visit);
visit(t->left);
if(t->right!=0)inorder(t->right, visit);
}
template <class Type>
void BinaryTree<Type>::reorder(void (*visit)(Type)){
if (root!=0)preorder(root, visit);
}
template <class Type>
void BinaryTree<Type>::reorder(TreeNode *t, void(*visit)(Type)){
visit(t->left);
if (t->left!=0)preorder(t->left, visit);
if(t->right!=0)preorder(t->right, visit);
}
template <class Type>
void BinaryTree<Type>::postorder(void (*visit)(Type)){
if (root!=0)postorder(root, visit);
}
template <class Type>
void BinaryTree<Type>::postorder(TreeNode *t, void(*visit)(Type)){
if (t->left!=0)preorder(t->left, visit);
if(t->right!=0)preorder(t->right, visit);
visit(t->left);
}