template problem with ostream operator (Borland compiler)

Discussion in 'C++' started by Ryan M. Keith, Oct 16, 2003.

  1. I am having a problem with the ostream operator in templated classes
    that I wrote (I'm using the Borland compiler), and I'm certain that
    the templates are the problem because when I remove the templating
    completely everything works perfectly. Here is the error message:


    Error: Unresolved external 'operator <<(std::basic_ostream<char,
    std::char_traits<char> >&, const BinomialTree<int>&)' referenced from
    C:\DOCUMENTS AND SETTINGS\RYAN\DESKTOP\COMPUTER SCIENCE\CS
    2413\PROJECTS\PROJECT 3\TEMP\TMPPROJ3.OBJ

    And here is the relevant code:

    template<class Object>
    class BinomialTree
    {
    friend ostream& operator<< (ostream& stream, const
    BinomialTree<Object>& tree);

    private:
    //array of Binomial Node pointers
    BinomialNode<Object>* locations[100];

    ....more stuff
    };

    template<class Object>
    ostream& operator<< (ostream& stream, const BinomialTree<Object>&
    tree)
    {
    for(int j = 0; j<100; j++)
    {
    if(tree.locations[j] != NULL)
    {
    stream<<(*(tree.locations[j]));
    }
    }
    return stream;
    }

    template <class Object>
    class BinomialNode
    {
    //overloaded ostream operator, for displaying
    friend ostream& operator<< (ostream& stream, const
    BinomialNode<Object>& node);

    private:
    void copy(const BinomialNode<Object>& rhs);
    Object _key;
    int _degree;

    BinomialNode* next; //pointer to the next node (a sibling)
    BinomialNode* down; //the pointer to the child
    BinomialNode* up; //the pointer to this node's parent

    ....more stuff
    };

    template<class Object>
    ostream& operator<< (ostream& stream, const BinomialNode<Object>&
    node)
    {
    stream<<node._key;

    if(node.down != NULL)
    return stream<<(*(node.down));

    else {

    if(node.next == NULL)
    return stream;
    else
    return stream<<(*(node.next));
    }
    }
     
    Ryan M. Keith, Oct 16, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan M. Keith
    Replies:
    0
    Views:
    298
    Ryan M. Keith
    Oct 16, 2003
  2. keit6736
    Replies:
    2
    Views:
    359
    Sergiy Kanilo
    Oct 17, 2003
  3. Rock
    Replies:
    4
    Views:
    444
    Jim Langston
    Nov 20, 2005
  4. Thomas Lenz
    Replies:
    3
    Views:
    495
    Thomas Lenz
    Oct 1, 2008
  5. Replies:
    2
    Views:
    1,930
Loading...

Share This Page