Linking error with friend function in template class

后端 未结 1 1965
慢半拍i
慢半拍i 2021-01-27 02:13

I have a linking problem when using a home-made Complex class.

Class definition:

template
class Complex
{
public:
    Complex(const T real         


        
1条回答
  •  终归单人心
    2021-01-27 02:34

    The function declared as

    template 
    class Complex {
        // ...
        friend T abs(Complex& z);
        // ...
    };
    

    is not a function template! It looks a bit like one as it is nested within a class template but that isn't quite enough. Here is what you probably meant to write:

    template  class Complex;
    template  T abs(Complex&);
    
    
    template 
    class Complex {
        // ...
        friend T abs(Complex& z);
        // ...
    };
    

    Alternatively, you could implement abs() when declaring it as friend.

    0 讨论(0)
提交回复
热议问题