C++ template friend function not linking

前端 未结 2 1545
轻奢々
轻奢々 2021-01-27 01:52

I have the following code which compiles in VC6 :

Text.h:

template 
class CTextT
{
   public:

    friend  CTextT add(const CTextT&         


        
2条回答
  •  花落未央
    2021-01-27 02:42

    Of course it does give a linker error. The add() function you made a friend isn't a function template! It is a non-template function declared inside a class template. As a result, you won't be able to define this function as a template, either. You can directly define it at the friend declaration.

    The alternative is to declare the function template prior to the class template. Of course, this requires a declaration of the class template, too. The following should work:

    template  class CTextT;
    template  CTextT add(CTextT const&, CTextT const&);
    template 
    class CTextT
    {
        friend CTextT add<>(CTextT const&, CTextT const&);
        // ...
    };
    

    Now the template definition you have give should work.

提交回复
热议问题