How to properly store template function definitions in a .cpp file in C++?
Hello! I'm learning C++ and encountered a problem that I can't solve for the second day. I'm writing a small template container class. I decided to separate the declaration and implementation into different files, as I usually do with regular classes. Here's an example:
```cpp
// MyArray.h
template
class MyArray {
private:
T* data;
int size;
public:
MyArray(int s);
~MyArray();
T& operator[](int index);
};
// MyArray.cpp
#include "MyArray.h"
template
MyArray::MyArray(int s) : size(s) {
data = new T[size];
}
template
MyArray::~MyArray() {
delete[] data;
}
template
T& MyArray::operator[](int index) {
return data[index];
}
// main.cpp
#include "MyArray.h"
int main() {
MyArray arr(10);
```
No answers yet.
Log in to leave an answer