The code below isn't working and I can't find out the reason why, any help would be much appreciated.
//In Maths.h file
template <class T> class Maths{
public:
Maths<T>(T lhs);
template<typename U>
Maths<T>(const Maths<U>& otherMaths);
~Maths();
template <typename U>
Maths<T>& operator+(const Maths<U>& rhs);
template <typename U>
Maths<T>& operator*(const Maths<U>& rhs);
template <typename U>
Maths<T>& operator-(const Maths<U>& rhs);
private:
T _lhs;
};
//In Maths.cpp file
#include "Maths.h"
template <class T>
Maths<T>::Maths(T lhs){
_lhs = lhs;
return _lhs;
}
template <class T> template <typename U>
Maths<T>::Maths(const Maths<U>& otherMaths){
_lhs = otherMaths._lhs;
}
template <class T>
Maths<T>::~Maths(){}
template <class T> template <typename U>
Maths<T> Maths<T>::operator+(const Maths<T>& rhs){ return Maths._lhs + rhs; }
template <class T> template <typename U>
Maths<T> Maths<T>::operator-(const Maths<T>& rhs){ return Maths._lhs - rhs; }
template <class T> template <typename U>
Maths<T> Maths<T>::operator*(const Maths<T>& rhs){ return Maths._lhs * rhs; }
The issue is in VS it is not recognizing the keyword operator (i.e. doesn't appear blue), why is this?
EDIT:
I have removed the errors pointed out below. Moved all definitions into .h file and the code still won't compile, errors found here: https://i.stack.imgur.com/fXGK5.png
new code (if interested):
//in Maths.h file
template <class T> class Maths{
public:
Maths<T>(T lhs);
template<typename U>
Maths<T>(const Maths<U>& otherMaths);
~Maths();
T& getValue(){ return _lhs; };
template <typename U>
Maths<T>& operator+(const Maths<U>& rhs);
template <typename U>
Maths<T>& operator*(const Maths<U>& rhs);
template <typename U>
Maths<T>& operator-(const Maths<U>& rhs);
private:
T _lhs;
};
template <class T>
Maths<T>::Maths(T lhs){
_lhs = lhs;
}
template <class T> template <typename U>
Maths<T>::Maths(const Maths<U>& otherMaths){
_lhs = otherMaths.getValue();
}
template <class T>
Maths<T>::~Maths(){}
template <class T>
Maths<T> Maths<T>::operator+(const Maths<T>& rhs){ return _lhs + rhs.getValue(); }
template <class T> template <typename U>
Maths<T> Maths<T>::operator-(const Maths<U>& rhs){ return _lhs - rhs.getValue(); }
template <class T> template <typename U>
Maths<T> Maths<T>::operator*(const Maths<U>& rhs){ return _lhs * rhs.getValue(); }
//in main.cpp
#include "Maths.h"
int main(){
Maths<int> x = 1;
Maths<int> y = 5;
x + y;
return 0;
}