templates and ld error (linking)

Hi,

I had previously programmed a vector and matrix template class library (put together in the same .h file). I used this as one of the header file to compile a set of header files in a directory which also uses this class. I obtained the object file and a *.a file (static library I suppose?). Then, I had to compile another directory which depends on all these compiled files and the linkage worked fine...

A couple days back, I wrote some widgets which operate on objects created using this class. I have 2 classes "vec" and "mat", for example, to find the index of certain element, or to transpose a matrix etc.. These were programmed as function templates (here once again, I made all function templates in 1 single header file). However, when I added this file to the list of headers and tried to compile.. it doesn't work. I get ld error. I tried putting them as separate header and .cpp files, even then it doesn't work.

How does one compile by linking a file containing function templates? I have wasted a lottttt of time already on this.. its irritating!!!!!!

Thank you very much.
If you have templates of anything, they must be in the header file.
Yes, that is what I know off too.
I have 3 header files,
1) the template class "ctl.h" for "vec" and "mat" in 1 header file (with their member function declarations, definitions).
2) a "types.h" file defining the typedefs for ctl
3) a "widgets.h" file containing all function templates which operate on both matrix and vector objects.

I wrote a small program "example.cpp" by including these files and compile using g++ example.cpp, it worked. I did this to verify the code initially. Now, I copied them to the directory which has the structure as follows:
1
2
3
directory a
directory b
widgets (new)

"directory a" creates the executable file by linking to a *.a object(??) created in "directory b". I am able to create the first step of creating this *.a file using "widgets" in "directory b". When I try to compile and create the executable it gives me "duplicate symbol found in " and shows me the first function I have used from "widgets.h" in the file (for example vec<int> sum(vec<int>&) ) and says "ld error 1 status exit".

Can anyone help me out with this problem please...
I didn't really got your problem. Templates are just not compiled into libraries (usually). They only reside in .h files and are compiled into each program where you use them again and again.

Since you seem to have only templates (vec, math, widget) here, why do you have any library file at all? What classes should it contain?


"directory a" creates the executable file by linking to a *.a object


BLING! BLING! BLING!

You got the "Achievment for using 'a' in three different contextes within one sentence."

:-D

Ciao, Imi.
Topic archived. No new replies allowed.