It does not want to be compiled. Problem overview:
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `main':
/mnt/c/Users/Pasha/CLionProjects/test/main.cpp:4: undefined reference to `box<int>::box()'
/usr/bin/ld: /mnt/c/Users/Pasha/CLionProjects/test/main.cpp:4: undefined reference to `box<int>::~box()'
collect2: error: ld returned 1 exit status
I had this issue before my program looks like this. Recently it worked but something went wrong and I got that. I deleted almost everything and got it easier as much as possible I check directory and setting
main.cpp
#include "box.h"
int main() {
box user_box;
return 0;
}
box.cpp
#include "box.h"
template<typename Type>
box<Type>::box(){
pp = new Type*[size_box];
for (int i = 0; i < size_box; i++){
pp[i] = new Type[size_box];
}
}
template <typename T>
box<T>::~box(){
for (int i = 0; i < size_box; i++){
delete[] pp[i];
}
delete[] pp;
}
and box.h
#ifndef USER_BOX_BOX_H
#define USER_BOX_BOX_H
#define size_box 10
class box{
private:
T** pp;
public:
box();
~box();
};
#endif //USER_BOX_BOX_H
I would be glad to get help and some explanation, other projects with the concept are right and the compiler does not complain. And IF I CHANGE .H TO .CPP IT WORKS. I dont understand