Error issued by 'extern template'
There is a template class with three of member functions without implementation. But at the end of the header file of the class, something like the following appears:
extern template void template_class::member_function();
When compiling and linking to generate this library, error lnk2001 is issued.
The description is:
Error LNK2001: unresolved external symbol "......."
I understand that this error occurs when a function lacking implementation or the compiler doesn't get the implementation for appropriate function. But the situation is different here. Because the source codes for the dll worked well when the writer wrote it. And furthermore, other member functions of the template class works fine without issuing any errors. In fact, these three functions do not be implemented indeed. But why was it correct, and not now?
Anybody who knows how to solove this kind of problem could tell me the how to?
Thank you very much!
Re: Error issued by 'extern template'
Quote:
Originally Posted by stoneyrh
There is a template class with three of member functions without implementation.
That is the start of the problem.
Quote:
When compiling and linking to generate this library, error lnk2001 is issued.
Of course, because the linker couldn't find the implementation. You even said there was no implementation.
Quote:
Because the source codes for the dll worked well when the writer wrote it.
OK, so I'll speak with this writer. Seriously, this doesn't give us any information in solving your problem.
Quote:
In fact, these three functions do not be implemented indeed.
So how does the linker know where the implementations are?
Quote:
Anybody who knows how to solove this kind of problem could tell me the how to?
Provide an implementation that the linker is looking for. I don't know what else to tell you, since you didn't really explain the problem well at all. Sorry.
Regards,
Paul McKenzie
Re: Error issued by 'extern template'
I am sorry for confusing you.
The class is similar to the follwing class:
template <typename T>
class TemplateClass
{
public:
TemplateClass(){};
~TemplateClass(){};
void func1(){};//with implementation and works well
void func2();//without implementation
};
extern template void TemplateClass<T>::func2();
When linking, error lnk2001 is issued.
In fact, the real class is a base class for other classes.
Re: Error issued by 'extern template'
Like Paul said, you must put implementation of template member functions inside of template class definition. Unfortunately, todays compilers do not support export keyword nor any other mechanism, which allows to put member function definition outside of a class.
We are sorry.
Re: Error issued by 'extern template'
I understand that the definition of a template class member function must be included in the template class. The keyworks 'extern template' seem strange. As said in this document , it is used to suppress the implicit instantiation of templates. I don't really understand what means 'suppress the implicit instantiation of templates'.
By the way, the second volume of Thinking in C++ proposes a way to seperate the declaration and definition of template class.
Re: Error issued by 'extern template'
Code:
extern template void TemplateClass<T>::func2();
I'm a bit hazy on explicit instantiations, but is that even legal?
I think the statement is trying to say "there is an explicit instantiation of this template function somewhere", so the linker will assume that it's hanging arouns in one of the object files. If there's no definition for that instantiation, though, then the linker's going to throw a wobbly.
I still don't see how it could work as written, though. "...TemplateClass<int>..." I could understand, but there's no type "T" to explicitly instantiate on.
Re: Error issued by 'extern template'
I see what goes wrong. The appropriate cpp file was missing. Those three functions were implemented there. That's the first time I encounter this kind of code, and it cost me almost four hours.
Graham is correct. 'extern template' indeed has the ability to seperate the declaration and definition of tempate member functions. But it is not so popular. Maybe most compilers do not support it.
Thank everybody who is concerned the post.