Date: Fri, 25 Sep 1998 10:45:24 -0300 (EST) From: "Renato F. Cantao" To: Shue-Cheng CHEN cc: djgpp AT delorie DOT com Subject: Re: A Problem with Template Class In-Reply-To: <360B4190.1C2DD19F@ohriki.t.u-tokyo.ac.jp> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk Hi Mr. Chen! I guess I've already had the same problem in the past. I don't know exactly why, but templates are not compiled in an usual way. It seems to me that every time you make a "templatization", I mean, when you try SimpleArray saf; SimpleArray sai; SimpleArray sad; the compiler creates code for each "new" data type. It creates a code for "SimpleArray", "SimpleArray" and so on ... That means that when you make a template file, it must be included, not compiled! Compilation takes place when you declare an object template. I would do, for instance (details ommited): // FILE: simarra.h ... template class SimpleArray { public: SimpleArray( const int& ); ... }; In another file: simarra.hpp // FILE: simarra.hpp ... template SimpleArray::SimpleArray( const int& s_ ): ...do some stuff here { ... } And finally ... // FILE: main.cc #include "simarra.hpp" <- ATENTION!!! I'm including the HPP!!! main() { SimpleArray s; ... and so on! return 0; } OK???? Let me know if it works! Renato F. CANTAO State University at Campinas Campinas - Brazil On 25 Sep 1998, Shue-Cheng CHEN wrote: > Hi! > > I made a main program "test.cc" as follows, > > #include > #include "simarra.h" // Header for SimpleArray > > int main() > { > SimpleArray a; > for (int i=1; i<=6; i++) { > a[i] = i * 1.1; > cout << a[i] << " "; > } > cout << endl; > return 0; > } > > It's strange that this main program only works when I placed all > implementations of the class SimpleArray in the same test.cc. > > When the implementations of the main program and the class were > separated in two different files, e.g., test.cc and SimArra.cc, > in the linking stage, I always got the messages > > 1.o: In function `main': > 1.cc:1: undefined reference to `SimpleArray::SimpleArray(void)' > 1.cc:2: undefined reference to `SimpleArray::SimpleArray(void)' > 1.cc:6: undefined reference to `SimpleArray::operator[](int)' > 1.cc:7: undefined reference to `SimpleArray::operator[](int)' > 1.cc:11: undefined reference to `SimpleArray::~SimpleArray(void)' > 1.cc(.text+0xc9): undefined reference to > `SimpleArray::~SimpleArray(void)' > 1.cc(.text+0xf3): undefined reference to > `SimpleArray::~SimpleArray(void)' > 1.cc(.text+0x113): undefined reference to > `SimpleArray::~SimpleArray(void)' > > Why did it become undefined? > > Thank you in advance. > > Shue-Cheng Chen >