CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2004
    Posts
    1

    template and friend ostream operator problem, please help

    I'm currently trying to convert a class into a template but am having problems with the overloaded ostream operator and the templates. I have the overloaded ostream function as a friend inside a class called AssocArray, I have tried everything that I can think of to get this function working as a template but it is being stubborn. I have uploaded a copy of the erros I'm recieving along with the code that portion of the code that is giving me trouble to a html file on the web to make things easy for everyone. Any help you could provide would be much appreciated.

    All the code and erros are here:
    http://bsdadmins.net/problems.html

    Dav

  2. #2
    Join Date
    Jul 2004
    Posts
    20

    Re: template and friend ostream operator problem, please help

    In AssocArray.h, you need to indicate that the operator<< function is a template function. This is done by placing "<>" after the function name, as follows:

    Code:
    template <class T1, class T2>
    class AssocArray {
    friend ostream& operator<< <> (ostream& s, const AssocArray<T1, T2> &); 
     
    //... etc
    This will at least fix some of the warnings (AssocArray.h:17), but I'm not sure about the others.

    Try it and see what you get.

    Just remember that it is always darkest just before it goes pitch black.

  3. #3
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: template and friend ostream operator problem, please help

    If you're template class needs to be access by other *.cpp files, then you need to move the template class implementation into your header file.

    I recommend you also move the implementation of your friend operator inside the class declaration.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured