CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    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!
    Stone

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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.
    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.
    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.

    In fact, these three functions do not be implemented indeed.
    So how does the linker know where the implementations are?
    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

  3. #3
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    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.
    Stone

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    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.
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    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.
    Stone

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Mar 2003
    Location
    Chongqing,China
    Posts
    71

    Smile 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.
    Stone

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured