Linking error with friend function in template class

后端 未结 1 1963
慢半拍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 <typename T>
    class Complex {
        // ...
        friend T abs(Complex<T>& 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 <typename T> class Complex;
    template <typename T> T abs(Complex<T>&);
    
    
    template <typename T>
    class Complex {
        // ...
        friend T abs<T>(Complex<T>& z);
        // ...
    };
    

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

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