Hi Eric, On Fri, Jun 10, 2011 at 04:48:01PM -0700, eric wrote: > Dear g++ programers: > > I tried to copy a simple code about template and class which use arry > of (int or double or char) > but I can not get pass compile(g++4.5.2, I install gcc 4.5.2 but not > gcc-g++ 4.5.2; however when I g++ -v, it still show it is 4.5.2 from > my old 4.4.3) from a book (C++Primer3rdEd, chapter2). Please help. Eric > ---------------------------------- > > eric@eric-laptop:~/CppPrimer3$ g++ Array.cpp pg52.cpp > /tmp/ccE9MPMg.o: In function `main': > pg52.cpp:(.text+0x23): undefined reference to `Array<int>::Array(int)' > pg52.cpp:(.text+0x37): undefined reference to > `Array<double>::Array(int)' > pg52.cpp:(.text+0x4b): undefined reference to `Array<char>::Array(int)' > collect2: ld returned 1 exit status This CAN'T work, because your code is illegal C++: you have to include the function definitions of the template-functions Array<>::Array(int) at least in one file where you use them. - when g++ compiles "Array.cpp", it does NOT GENERATE ANY CODE -- because no template is used here - when g++ compiles "pg52.cpp", it suddenly learns "Oh, I need the class Array<int>" -- but then it won't read again Array.cpp to learn how those funcions are defined & generate them to pass them to the linker ==> you get the undefined references - errors... The easiest solution is to change your file "pg52.cpp": replace #include "Array.h" by #include "Array.cpp" and compile the program as g++ pg52.cpp (without mentioning Array.cpp on the commandline) This way, while g++ compiles pg52.cpp, it also knows all the contents of Array.cpp and thus will generate the necessary template functions. Some gcc-specific information about template instantiation can be found here: http://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html HTH, Axel