Templated Friend's???

Started by
4 comments, last by joeG 23 years, 10 months ago
What would be the proper syntax for declaring a templated friend method? This is what I have so far #include < iostream.h > template < class T> class JGTuplet { friend ostream& operator < <(ostream&, const JGTuplet < T > &); . . . The rest of the class }; This would have worked in BCB 4.0 Pro but Linux gcc complains that the above code does not declare a templated friend function. This is the way I've learned how to do it. It suggests having the template function already declared, and placing a < Type > in front of the opening function parenthesses (after the operator<< in the above code). What gives? Thanks, joeG Edited by - joeG on 6/16/00 3:36:03 PM
joeG
Advertisement
I can't say for sure, but in DJGPP you'd write:

    friend ostream& operator<< <> ( ostream&, const JGTuplet<T>& );    


to make it work. Maybe worth a shot?

/TP



Edited by - suburber on June 16, 2000 5:45:09 PM
*********TP"A fanatic is one who can't change his mind and won't change the subject"-- Winston Churchill
quote:Original post by suburber

I can''t say for sure, but in DJGPP you''d write:

        friend ostream& operator<< <> ( ostream&, const JGTuplet<T>& );    [/source]to make it work. Maybe worth a shot?/TPEdited by - suburber on June 16, 2000 5:45:09 PM     <hr height=1 noshade></BLOCKQUOTE></font> Um, didn''t work.  Of what I gathered g++ requires you to do something like this[source] template <class T>friend ostream& operator<< <T> (ostream&, const JGTuplet<T>&);    


I''ll try it without the T in the middle of the template specifiers between "operator<<" and "(ostream &..."

joeG
Um, didn''t work. Of what I gathered g++ requires you to do something like this

     template <class T>friend ostream& operator<< <T> (ostream&, const JGTuplet<T>&);        


I''ll try it without the T in the middle of the template specifiers between "operator<<" and "(ostream &..."

joeG

Sorry about how ugly the above post turned out. I wouldn''t mind at all if it got deleted *Hint, hint*
joeG
Um, finally hacked my way to the solution...

In gcc (g++, c++) you'll have to do something like this.


In file, x.h


                template <class Type> class X;template <class Type> ostream& operator<<(ostream&, X<Type>&);template <class Type>class X{private:protected:public:	friend ostream& operator<< <Type> (ostream &, const X<Type>&);};	[/source]<hr><h1>In file, x.cpp</h1> [source]template class X<int>;template class X<long>;template class X<float>;template class X<double>;template class X<char*>;template class X<bool>;template ostream& operator<<(ostream&, X<int>);template ostream& operator<<(ostream&, X<long>);template ostream& operator<<(ostream&, X<float>);template ostream& operator<<(ostream&, X<double>);template ostream& operator<<(ostream&, X<char*>);template ostream& operator<<(ostream&, X<bool>);template <class Type>ostream& operator<<(ostream&, X<Type>){	//implementation}[/source]<hr><h1>in file, main.cpp</h1>[source]#include "x.h"int main(int argc, char **argv){	X<int> a;	/* 	The following line would cause an error because	you didn't explicitly declare to gcc (or the linker) 	that you expect a template type of `unsigned char'	*/	X<unsigned char> b;	.	.	.	return 0;}            


I hope this will prove helpful to some of the people trying to work with Linux/Unix gcc.

joeG



Edited by - joeG on June 19, 2000 10:00:38 AM
joeG
I''ve given up trying to fix the above post, I think you all will get the idea, but if one of the moderators would be so kind, I''d be very grateful.

joeG
joeG

This topic is closed to new replies.

Advertisement