Turning into a template

I have been working on this question for ages and i keep geetting it to the point with no issues, but then it wont complie due to unresolved externals(The worst things ever!). i Just cant figure out what im doign wrong. All i have to do is convert it from being a very standard class and function to then have template abilities so it can be used for other data types. It's just annoying me and i cant get it to run!

#include <iostream>
using namespace std;

//template <class T>
class cpair
{
public:
cpair(int x = 0, int y = 0) {
A = x; B = y;
}
void print()
{
cout << A << " " << B << endl;
}
int A, B;
};


template <typename T>
void Add(T A1, T &A2, T& R)
{
R.A = A1.A + A2.A;
R.B = A1.B + A2.B;
}
template <typename T>
int main()
{
cpair A1(4, 5), A2(1, 3), &result;
Add(A1, A2, result);
result.print();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

template <class T>
  struct cpair
  {
    T a, b;
    
    explicit cpair(T x = T{}, T y = T{}): a(x), b(y) {}   
    void print() const { std::cout << a << ' ' << b << '\n'; }
 };


template <typename T>
  void add(cpair<T> const& x, cpair<T> const& y, cpair<T>& result)
  {
    result.a = x.a + y.a;
    result.b = x.b + y.b;
  }

int main()
{
  cpair<int> a1(4, 5), a2(1, 3), result;
  add(a1, a2, result);
  result.print();
}
Last edited on
Topic archived. No new replies allowed.