Why must C++ templates be implemented in header files? Undefined reference error
Hello! I'm a beginner in C++ and trying to understand templates. I have a simple project with three files: `main.cpp`, `math_utils.h` and `math_utils.cpp`.
In the header file I declared a template function:
```cpp
// math_utils.h
#ifndef MATH_UTILS_H
#define MATH_UTILS_H
template
T max(T a, T b);
#endif
```
In the implementation file I wrote:
```cpp
// math_utils.cpp
#include "math_utils.h"
template
T max(T a, T b) {
return (a > b) ? a : b;
}
```
And in `main.cpp` I use:
```cpp
// main.cpp
#include
#include "math_utils.h"
int main() {
std::cout << max(5, 10) << std::endl;
return 0;
}
```
When compiling I get a linker error: `undefined reference to 'int max(int, int)'`. I've read that templates