Hi
I'm a beginner.
I'm struggling with class templates and can't seem to find my error.
//Header File (Calculator.h)
template <class T>
class Calculator
{
private:
T first;
T second;
public:
Calculator();
Calculator(T,T);
void setA(int a){first = a;}
void setB(int b){second = b;}
T getA(){return first;}
T getB(){return second;}
T sum();
T difference();
T product();
T quotient();
};
//Implementation file Calculator.cpp
#include "Calculator.h"
#include <iostream>
using namespace std;
template<class T>
Calculator<T>::Calculator()
{
}
template <class T>
Calculator<T>::Calculator(T a, T b)
{
first = a;
second = b;
}
With template classes, you need to put the definitions of the methods in the header file. It's annoying, but you need to do it, because the full definition of the class needs to be available to the compiler at the point where it compiles the code that uses the template.
template <class T>
class Calculator
{
private:
T first;
T second;
public:
Calculator();
Calculator(T,T);
void setA(int a){first = a;}
void setB(int b){second = b;}
T getA(){return first;}
T getB(){return second;}
T sum();
T difference();
T product();
T quotient();
};
#include "calculator.inl"
#include <iostream>
template <class T>
class Calculator
{
private:
T first;
T second;
public:
Calculator()
{
// define inside the class
}
Calculator(T,T)
{
// define inside the class
}
void setA(int a)
{first = a;}
void setB(int b)
{second = b;}
T getA()
{return first;}
T getB()
{return second;}
T sum()
{
// define inside the class
}
T difference()
{
// define inside the class
}
T product()
{
// define inside the class
}
T quotient()
{
// define inside the class
}
};