templates with pointers

This is a discussion on templates with pointers within the C++ Programming forums, part of the General Programming Boards category; i'm trying to construct binary trees and BST's with templates. is that a bad idea? I'm getting template erros when ...

  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,699
    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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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,699
    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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21