From: Farooq Azam Newsgroups: comp.os.msdos.djgpp Subject: [HELP]: Template Question Date: Sun, 03 Dec 2000 19:13:48 -0500 Organization: Virginia Tech, Blacksburg, Virginia, USA Lines: 93 Message-ID: <3A2AE1BC.CEE72E58@vt.edu> NNTP-Posting-Host: h80ad2791.async.vt.edu Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: solaris.cc.vt.edu 975888795 26648 128.173.39.145 (4 Dec 2000 00:13:15 GMT) X-Complaints-To: abuse AT vt DOT edu NNTP-Posting-Date: 4 Dec 2000 00:13:15 GMT X-Mailer: Mozilla 4.75 [en] (Windows NT 5.0; U) X-Accept-Language: en,zh-CN To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Hello, I need some help/sugegstions with compilation of the following code. It compiles fine under MS-VC++ 6 and does not under g++ version 2.952. Thanks in advance for any suggestions. -Farooq -- Errors-- D:\DOWNLO~1\TEMPLA~1>g++ -o testoperator test.cpp In file included from testimpl.cpp:1, from test.cpp:1: testspec.h:8: warning: friend declaration `class ostream & operator <<(ostream & , const Complex &)' testspec.h:8: warning: declares a non-template function testspec.h:8: warning: (if this is not what you intended, make sure testspec.h:8: warning: the function template has already been declared, testspec.h:8: warning: and add <> after the function name here) testspec.h:8: warning: -Wno-non-template-friend disables this warning. d:/djgpp/tmp\ccJvza2q.o(.text+0x38):test.cpp: undefined reference to `operator<< (ostream &, Complex const &)' d:/djgpp/tmp\ccJvza2q.o(.text+0x5f):test.cpp: undefined reference to `operator<< (ostream &, Complex const &)' collect2: ld returned 1 exit status --- --- Code: test.cpp --- #include "testimpl.cpp" int main(void) { int a=2; int b=3; Complex C1(a,b); cout << C1; ComplexC2(C1); cout << C2; return 0; } --- --- Code: testspec.h --- #ifndef _TESTSPEC_H #define _TESTSPEC_H #include #include template class Complex { friend ostream& operator << (ostream &output, const Complex &complex); public: Complex(T real, T imaginary); Complex(const Complex &complex); private: T real; T imaginary; }; #endif --- --- Code: testimpl.cpp --- #include "testspec.h" template Complex::Complex(T nreal, T nimaginary) { real=nreal; imaginary=nimaginary; } template Complex::Complex(const Complex &complex) { real=complex.real; imaginary=complex.imaginary; } template ostream &operator << (ostream &output, const Complex &complex) { output << "Complex Number..." << endl; output << "Real part .. " << complex.real << endl; output << "Imaginary part ..." << complex.imaginary << endl; output << endl; return output; } ---