I'm trying to write some code for simple complex number manipulation. I'm using a template class and I'm having trouble overloading operators (specifically +,-,*,/). I'm trying to declare the overload inside my template class, and then define them after in the same header file.
My header code is as follows:
#ifndef MY_CLASS_H
#define MY_CLASS_H
template <class T> class complex
{
private:
T re,im;
public:
// Constructors & destructor
complex(){re=im=0;}
complex(T r, T i){re=r; im=i;}
~complex(){}
// Return real component
T realcomp() const {return re;}
// Return imaginary component
T imagcomp() const {return im;}
// Overload + operator for addition
complex<T> operator+(const complex<T> &C);
....
};
#endif
#include<iostream>
#include<cmath>
using namespace std;
template <class T> complex<T>& complex<T>::operator+(const complex &C){
complex<T> A(re+C.realcomp(),im+C.imagcomp());
return A;
}
This returns errors that I have so far been unable to solve and I'm not entirely sure where I've gone wrong. A mixture of me being a beginner to C++ and trying to puzzle together solutions to other issues on this website has probably meant that my code is a bit of a mess - I apologise!
Any help would be greatly appreciated.