I managed to to anassignment for school and everything is working.
I made all the functions in the header file and I planned to move them to a cpp file once everything is working. But when I leave the definition of my function in the header file and put the functions body in the cpp file it just doesnt work.
I get this error : Undefined reference to ...
It tells me that it cant find the function.
It is a makefile project and the makefile was provided by the teacher and it generates .o files of the classes.
I import my .h file in my main file. When everything is in this header file it works perfectly but not anymore when I move it to the .cpp file.
I have the feeling my makefile is wrong but I cant figure out what is it.
Here is the makefile : https://pastebin.com/T4MmzpcK
The file in questions are MyArray.h and MyArray.cpp.
I will work on solving the problem so I am available immediately if you have any more questions
[edit] Here is more info :
Let's take for exemple the function defined in this .h
file : https://pastebin.com/pTjNGtFb
And the code is in this .cpp
file : https://pastebin.com/jhLae9Ps
When I call the function I get the error :
obj/test.o: In function `main':
test.cpp:(.text+0x665): undefined reference to `MyArray<int>::elementAt(unsigned int)'
test.cpp:(.text+0x69b): undefined reference to `MyArray<int>::elementAt(unsigned int)'
test.cpp:(.text+0x77f): undefined reference to `MyArray<int>::elementAt(unsigned int)'
test.cpp:(.text+0x792): undefined reference to `MyArray<int>::elementAt(unsigned int)'
test.cpp:(.text+0x7a9): undefined reference to `MyArray<int>::elementAt(unsigned int)'
obj/test.o:test.cpp:(.text+0x7bc): more undefined references to `MyArray<int>::elementAt(unsigned int)' follow
collect2: error: ld returned 1 exit status
make: *** [bin/test] Error 1
I have been looking for hours but I cant find what is wrong.
It works when I put
template class MyArray<int>;
At the end of the cop file but it seems so wrong to do so