From: "Stefan Schimanski" Newsgroups: comp.os.msdos.djgpp Subject: template classes with DJGPP Date: 4 Aug 1997 20:11:46 GMT Organization: Metronet Kommunikationsdienste GmbH & Co. KG, Germany Lines: 174 Message-ID: <01bca10d$bb446600$0100a8c0@stefan.schimanski> NNTP-Posting-Host: braunschweig2.pop.metronet.de To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Hi, I've implemented the following template class, that simulates a dynamic array. Although I've put the whole definition of the class in the header file, the linker displays errors. The compiler works fine. The linker can't find the constructor definition: Error: contest.o: In function `global constructors keyed to C': contest.cc(24) Error: undefined reference to `CContainer::CContainer(int)' There were some errors I've removed the parameter of the constructor an tried again. But I got the same error. I know that some parameter for gcc exist to control the way it handles template. Would some of them help? ------------------------------------------------------------------- /* CONTEST.CC */ #include #include "contain.h" CContainer C(16); void main() { for (int a=0; a<10; a++) C.Insert(a); for (int b=9; b>=0; b++) cout << C[b] << endl; } ------------------------------------------------------------------- /* CONTAIN.H */ #ifndef CONTAIN_H_INCLUDED #define CONTAIN_H_INCLUDED #include template class CContainer { public: CContainer(int aMin=4); virtual ~CContainer(); int Insert(T aItem); void Remove(int aItem); protected: T *First; int NextItem; int Min; int Size; void CheckSize(); void ChangeSize(int aSize); public: inline T operator[](int i) const { return *(First+i); } }; //========================================================== template CContainer::CContainer(int aMin) { NextItem = 0; Size = 0; First = NULL; Min = 4; ChangeSize(Min); } template CContainer::~CContainer() { delete[] First; } template int CContainer::Insert(T aItem) { *(First+NextItem) = aItem; int Num = NextItem; NextItem++; CheckSize(); return Num; } template void CContainer::Remove(int aItem) { *(First+aItem) = NULL; if (NextItem-1 == aItem) { NextItem--; } CheckSize(); } template void CContainer::CheckSize() { if (NextItem>=Size) { ChangeSize(Size*2); } else if (NextItem < (Size>>1)) { ChangeSize(Size >> 1); } } template void CContainer::ChangeSize(int aSize) { // Check new size int NewSize = aSize; if (NewSize