Thread: templates with pointers

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    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.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Question ????

    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>:ostorder' : 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>: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>:ostorder(void (*visit)(Type)){
    if (root!=0)postorder(root, visit);
    }

    template <class Type>
    void BinaryTree<Type>:ostorder(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

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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);
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Object pointers and Templates
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2004, 08:41 AM
  3. Vectors of pointers and function templates
    By 7words in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2004, 11:39 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM