Date: Thu, 27 Jan 1994 12:17:39 +0100 From: "Pawel Cichosz" To: djgpp AT sun DOT soe DOT clarkson DOT edu Subject: Using templates with DJGPP Hi, My problem is G++ problem rather than just DJGPP problem, but I am sure some of DJGPP users will be able to help me. I have just installed DJGPP on my system and tried to use it. I have problems with C++ programs using templates. Consider the following example, as simple as possible. ------------------------------------------------- File dummy.h: ------------------------------------------------- #include template class Dummy { private : T t; public : Dummy (const T &t) : t (t) {} friend istream & operator >> (istream &, Dummy &); friend ostream & operator << (ostream &, const Dummy &); }; template istream & operator >> (istream &is, Dummy &d) { return is >> d.t; } template ostream & operator << (ostream &os, const Dummy &d) { return os << d.t; } -------------------------------------------- File main.cc: -------------------------------------------- #include "dummy.h" int main (void) { Dummy d (999); cin >> d; cout << d; return 0; } Running gcc -o main main.cc -lgpp I get a linker message about undefined symbol 'istream & operator >> (istream &, Dummy &)' (as a matter of fact, ld reports this name in an encoded form), while the other operator (<<) is defined. I looked into cc1plus's assembler output and found that in fact one operator function was generated and the other was not. I suppose (and I hope !) that I am doing something wrong. Any ideas ? I would also appreciate a more general advice on how to use templates with DJGPP (G++) in an efficient and portable way. I have source files that make an extensive use of templates, which compiled and linked successfully with BC++ 3.1. I hope I am not bothering you too much. Regards, Pawel P.S. When I define both operators inside class declaration, it works. Also, when I change the second parameter's type for operator >> () adding 'const': istream & operator >> (istream &, const Dummy &) (this makes its definition illegal, but the compiler produces only a warning), it works. Isn't that strange ? P.S.(2) (Perhaps more relevant to DJGPP itself) When I remove #include directive from dummy.h, I get a segmentation violation. But perhaps something is wrong in my configuration.