Click to See Complete Forum and Search --> : template and friend ostream operator problem, please help


DRLski
November 29th, 2004, 11:38 PM
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

DallasZimmer
November 29th, 2004, 11:57 PM
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:


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.

Axter
November 30th, 2004, 01:50 PM
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.