Not sure why you added <int> here?
template <typename T>
Test<int>::Test(T num1)
{
number = num;
}
template <typename T>
Test<int> operator+(Test<T> &a, Test<T> &b)
{
return Test(a.number + b.number);
}
I'm no template expert but you can take a look at my code below
#include <iostream>
#include <cstring>
struct mys
{
double a, b;
};
std::ostream& operator <<(std::ostream & out, const struct mys & s)
{
return out << s.a << " " << s.b;
}
struct mys operator +(const struct mys & a, const struct mys & b)
{
struct mys temp;
memcpy(&temp, &a, sizeof(struct mys));
temp.a += b.a;
temp.b += b.b;
return temp;
}
template <typename T>
class my_t
{
public:
my_t():the_value() {}
my_t(T val):the_value(val) {}
my_t operator +(const my_t & a) const
{
return this->the_value + a.the_value;
}
my_t& operator =(const my_t & a)
{
if ( this != &a )
{
this->the_value = a.the_value;
}
return *this;
}
T getitsvalue() const { return the_value; }
private:
T the_value;
};
template <typename T>
std::ostream& operator <<(std::ostream & out, const my_t<T> & m)
{
return out << m.getitsvalue();
}
int main()
{
struct mys f = {123.0, 234.0}, g = {34.0, 45.0};
my_t<struct mys> one(f);
my_t<struct mys> two(g);
my_t<struct mys> ans;
ans = one + two;
std::cout << ans << std::endl;
return 0;
}
Note I have a assignment operator=().