Question

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

Class definition:

template<class T>
class Complex
{
public:
    Complex(const T real = 0, const T imag = 0);
    Complex(const Complex<T>& other);
    ~Complex(void) {};

    Complex<T> operator*(const Complex<T>& other) const;
    Complex<T> operator/(const Complex<T>& other) const;
    Complex<T> operator+(const Complex<T>& other) const;
    Complex<T> operator-(const Complex<T>& other) const;

    friend void operator*=(const Complex<T>& z,const Complex<T>& other);
    friend void operator/=(const Complex<T>& z,const Complex<T>& other);
    friend void operator+=(const Complex<T>& z,const Complex<T>& other);
    friend void operator-=(const Complex<T>& z,const Complex<T>& other);
    void operator=(const Complex<T>& other);

    friend T& real(Complex<T>& z);
    friend T& imag(Complex<T>& z);
    friend T abs(Complex<T>& z);
    friend T norm(Complex<T>& z);
private:
    T real_;
    T imag_;
};

Implementation of abs:

template<class T>
T abs(Complex<T>& z)
{
    return sqrt(z.real_*z.real_ + z.imag_*z.imag_);
}

I use the function abs like this : if(abs(z) <= 2).

Here are some errors I get:

Error   4   error LNK2001: unresolved external symbol "long double __cdecl abs(class Complex<long double> &)" (?abs@@YAOAAV?$Complex@O@@@Z) C:\Users\Lucas\Documents\Visual Studio 2012\Projects\Fractals\Fractals\Main.obj Fractals
Error   3   error LNK2001: unresolved external symbol "long double & __cdecl imag(class Complex<long double> &)" (?imag@@YAAAOAAV?$Complex@O@@@Z)   C:\Users\Lucas\Documents\Visual Studio 2012\Projects\Fractals\Fractals\Main.obj Fractals

I get the same errors when using Complex<float> instead of Complex<long double>. I work with Visual C++ 2012. I would be really glad if you give me some hint on how to fix this. Thank you.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top