From: adsgohere AT durchnull DOT de (Rudolf Polzer) Newsgroups: comp.os.msdos.djgpp Subject: Re: template problem in djgpp References: <9fvfgf$obj$1 AT news DOT lth DOT se> X-DAUs: lesen Headers, ohne zu wissen, was das ist X-Homepage: http://www.durchnull.de User-Agent: slrn/0.9.6.3 (Linux) Date: Mon, 11 Jun 2001 18:39:36 +0200 Message-ID: <88s2g9.vi2.ln@durchnull.de> Lines: 65 NNTP-Posting-Host: 213.7.25.188 X-Trace: 992277897 news.freenet.de 29731 213.7.25.188 X-Complaints-To: abuse AT freenet DOT de To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Jason Green wrote: > "Tomas Nilsson" wrote: > > > Hello, > > I have a problem with templates i djgpp 2.03. I have tried the following > > program: > > Put the class declaration *and* implementation in the header: > > > file class1.h: > > template > > class class1 { > > private: > > TYPE T; > > public: > > void f1(); > > }; > > > template void class1::f1() {} > > > > file main.cpp > > #include"class1.h" > > int main() { > > class1 c1; > > c1.f1(); > > } > > > > The program compiles without errors and warnings, but then something goes > > wrong in the > > linking process. The linker ld in djgpp returns the error message "undefined > > reference to class::f1(void)". This is very strange, because the UNIX > > compiler CC can compile and build this program without any problem, and CC > > one of the best approximation of the C++ standard I know. > > Can anyone see what could be causing this problem > > gcc needs to see the class definition at compile time, otherwise > class1 does not exist at link time. > > > and why it works in CC and not djgpp? > > gcc DTRT here, so strictly speaking the other CC is buggy. The standard says here: you need to export the template to make the thing work: file class1.h: template class class1 { private: TYPE T; public: void f1(); }; file class1.cpp: export template void class1::f1() {} But the standard does not require support of the 'export template' construct because it is not very easy to implement without looking at the source (just using .o files). So the other compiler is buggy (it exports the template even if the word 'export' is missing), and gcc is right. -- #!/usr/bin/perl -w -- sequences $0=++$|;for(;;){print-length$0;$0=~s/(.)\1*/$1.length$&/ge;}print"\n";