Xref: news-dnh.mv.net comp.os.msdos.djgpp:3360 Path: news-dnh.mv.net!mv!barney.gvi.net!news.sprintlink.net!news.uoregon.edu!mayonews.mayo.edu!newsdist.tc.umn.edu!umn.edu!spool.mu.edu!pravda.aa.msen.com!simtel!news.kei.com!newsfeed.internetmci.com!in2.uu.net!news.usa.net!news.usa.net!not-for-mail From: heintze AT usa DOT net (Siegfried Heintze) Newsgroups: comp.os.msdos.djgpp Subject: Multiple definition of function with templates Date: 16 Nov 1995 01:12:23 -0700 Organization: Internet Express (800-592-1240 customer service) Lines: 62 Nntp-Posting-Host: earth.usa.net To: djgpp AT sun DOT soe DOT clarkson DOT edu Dj-Gateway: from newsgroup comp.os.msdos.djgpp I have a problem with template programs. Consider the attached files when compiled and linked: gcc -c func.cc gcc -c main.cc gcc func.o main.o -lgpp func.cc(.text+0x42): multiple definition of `Array::operator[](int)' main.cc(.text+0x4c): first defined here How do I instantiate a template and link it only once? Other DOS and UNIX linkers throw out redundant function bodies generated by template functions and template classes, but apparently the GNU one does not. Thanks, Sieg ------------------------------------------------------------------- // array.h #ifndef ARRAY #define ARRAY template class Array{ public: T& operator[](int i); private: T _data[S]; }; #endif // array.cc #include "array.h" template T& Array::operator[](int i) { return _data[i]; } // func.cc #include #include "array.cc" void func(){ Array x; cout << "function func()"< #include "array.cc" void func(); main(){ cout << "Begin"< x; func(); return 0; } -- ------------------------------------------------------------------------------- Siegfried Heintze heintze AT usa DOT net 3600 Berkley Avenue 72740 DOT 1365 AT compuserve DOT com Boulder, CO 80303 Voice: 303 499 6575