I'm pretty new to C++ and I'm learning how to use templates. I made this simple code here with a template class to compare 2 "T" variable and return the highest value.
TemplateClass.h
:
#pragma once
#include <iostream>
using namespace std;
template <class T>
class TemplateClass
{
private:
T a, b;
public:
TemplateClass() {}
TemplateClass(T first, T second)
{
a = first; b = second;
}
T getMax();
T getMin();
};
TemplateClass.cpp
:
#include "TemplateClass.h"
template <class T>
inline T TemplateClass<T>::getMax()
{
T result;
result = a > b ? a : b;
return result;
}
template <class T>
inline T TemplateClass<T>::getMin()
{
T result;
result = a < b ? a : b;
return result;
}
When I'm calling the getMax
method in my main.cpp
, I get an error. (LNK2019 unresolved external error)
main.cpp
:
int main() {
TemplateClass<int> myobject(100, 75);
cout << myobject.getMax();
return 0;
}
When I put all my code in one cpp file (main.cpp for example), it works.