template<typename T>
class TestClass
{
public:
void Print( T test );
};
template<typename T> void TestClass<T>::Print( T test )
{
cout<<test<<endl;
}
int main()
{
TestClass<int> test_class;
test_class.Print( 2 );
return 0;
}
Templates, VS 2005, multiple files.
Ok, I'm not too hot at writing my own templated classes, so bear my ignorance.
Essentially, I have problems when splitting things up into multiple files. Lets take a simple example:
This works, all well and dandy. Now, if I just split that up into three files (a header with the class declaration, a source file with the implementation of the member function, and another source file with the main function) I get an "unresolved external error for void TestClass<int>::Print(int)". Now, I'm actually including the files and such, all being compiled and linked together - I'm not being that braindead - hopefully.
Is there any reason why that shouldn't work? I'm at a loss here...
Unless your compiler supports the export keyword, and MSVC doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)
For more details see these articles: "Export" Restrictions, Part 1 and "Export" Restrictions, Part 2.
For more details see these articles: "Export" Restrictions, Part 1 and "Export" Restrictions, Part 2.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement