binary trees and protected data

This is a discussion on binary trees and protected data within the C++ Programming forums, part of the General Programming Boards category; this ones has keep me up all night long binTNode.h defines the nodes with a constructor that looks something like ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    5

    binary trees and protected data

    this ones has keep me up all night long

    binTNode.h defines the nodes with a constructor that looks something like this.
    Code:
    public:
    binTNode(const T& =T(), binTNode<T>* =0, binTNode<T>* =0);
    binTree.h is a friend of binTNode.h and from what i assume should be able to create a new node like so.
    Code:
    root = new binTnode<T>();
    however this error comes up.
    Code:
    Undefined                       first referenced
     symbol                             in file
    binTNode<int>::binTNode[in-charge](int const&, binTNode<int>*, binTNode<int>*)prog3.o
    ld: fatal: Symbol referencing errors. No output written to prog3.exe
    collect2: ld returned 1 exit status
    bash-3.00$
    also the bin tree class creates a
    Code:
    binTNode<T>* root;
    pointer without a problem. as for now im fried. any suggestions appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    binTree needs to be able to see the definitnion of binTNode(), not just the prototype. Thus
    Code:
    template<class T>
    binTNode<T>::binTNode(const T& d=T(), binTNode<T>* l=0, binTNode<T>* r=0) : data(d), left(l), right(r) {}
    needs to be in the header(basically)
    Normally with templates I go with nested classes and inline functions whenever I can.
    Code:
    template<class T>
    class tree {
        struct node {
            T data;
            node *l,*r;
            node(const &d,node *ll=0, node *rr=0) : data(d), l(ll), r(rr) {}
        } *root;
    public:
        ...
    };
    Note: not compiled, milage may vary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-06-2005, 08:23 AM
  2. binary search trees
    By sweets in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2004, 11:02 AM

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