Maybe your question is related to the methods of the template and not the actual template instances (like obj1 and obj2). All used methods of the template get compiled into the object file of all compilation units that use them. This is not a template related "problem", it is a more general problem related to inline functions and methods residing in public headers visible by two or more compilation units.
Example inline functions/methods in a header:
inline void MyGlobalFunc()
{
}
class MyClass
{
public:
MyClass()
{
}
void Method1()
{
}
inline void Method2()
{
}
void Method3();
void Method4();
};
void MyClass::Method3()
{
}
inline void MyClass::Method4()
{
}
The same inline function/method is allowed to be present in many object files and the linker will keep only one of these. There is only one big problem!!! The linker compares the inline methods only by their signature! If you succeed to put two inline methods or functions into two different object files with the same signature but with different implementation then your stuff will link but only one of the implementations will be kept by the linker (randomly) so one of your object file will call the implementation found in the other one resulting in wrong behavior and sometimes very hard to find bugs/crashes!!! Its easy to put inline functions/methods with different implementation into two object files, this usually happens without using headers! Let me demonstrate that:
x1.cpp:
#include <stdio.h>
struct SInlineTest
{
SInlineTest()
{
printf("%s 1\n", __FUNCTION__);
}
~SInlineTest()
{
printf("%s 1\n", __FUNCTION__);
}
};
inline void InlineFunc()
{
printf("%s 1\n", __FUNCTION__);
}
void InlineTest()
{
SInlineTest t;
InlineFunc();
}
x2.cpp:
#include <stdio.h>
struct SInlineTest
{
SInlineTest()
{
printf("%s 2\n", __FUNCTION__);
}
~SInlineTest()
{
printf("%s 2\n", __FUNCTION__);
}
};
inline void InlineFunc()
{
printf("%s 2\n", __FUNCTION__);
}
void InlineTest2()
{
SInlineTest t;
InlineFunc();
}
int main()
{
void InlineTest();
InlineTest();
printf("-----------------\n");
InlineTest2();
return 0;
}
Here the linker will keep only one of the InlineFunc() versions and the same is true for the constructors and the destructors. The linker will randomly throw out one of the versions. In case of class/struct declarations found inside .cpp files its advisable to put these class declarations into an anonymous namespace to avoid the linker conflict, in case of inline global function remove the "inline" keyword and either put the function into an anonymous namespace or use the static keyword or both is also valid.
EDIT: I've encountered this bug only twice in my life in case of bigger projects. This bug usually happens when two people use some small simple structs in two different .cpp files with the same name (like SItem - this is a popular name along with some other ones) and usually the inline ctor or dtor of these structs conflict resulting in a linking but misbehaving binary.