C++ template friend function not linking

前端 未结 2 1573
轻奢々
轻奢々 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:40

    The solution is following:

    template 
    class CTextT
    {
    public:
    
    template 
    friend CTextT add(CTextT const&, CTextT const&) ;
    
    friend  CTextT operator+(const CTextT& string1, const CTextT& string2)  
    {  return ::add(string1, string2); }
    
    };
    
    template 
    CTextT add(CTextT const&, CTextT const&) 
    {
       CTextT temp ;
       return temp ;
    }
    

    I found the following link to MSDN explaining how to add friend functions in templates - http://msdn.microsoft.com/en-us/library/f1b2td24.aspx.

    I tested and it works for VS2010 and VS2012. VC6 does not need the second template declaration (it takes it from the class) and in VC6 this code will not compile anymore - will return INTERNAL COMPILER ERROR.

提交回复
热议问题