0

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

TheMackou
  • 123
  • 1
  • 2
  • 11
  • It's difficult to tell; what function is "missing", and which file refers to it? Does the "missing" function use templates? How did you move the missing function to your .cpp file? – slugonamission May 03 '20 at 15:25
  • 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. – TheMackou May 03 '20 at 16:00
  • I expect your bug is here: `MyArray.cpp` specifically [https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – drescherjm May 03 '20 at 16:05
  • ***it works perfectly but not anymore when I move it to the .cpp file.*** Its not supposed to work as a .cpp file. – drescherjm May 03 '20 at 16:07
  • Of course I keep the prototype in the header – TheMackou May 03 '20 at 16:08
  • Keep the implementation in the header. Get rid of the .cpp file. Or use the "Alternative Solution" from here: [https://stackoverflow.com/a/495056/487892](https://stackoverflow.com/a/495056/487892) – drescherjm May 03 '20 at 16:09
  • So no cpp file ? But isnt it bad practice to put everything in the header ? – TheMackou May 03 '20 at 16:10
  • 1
    Please read this whole question to understand the problem: [https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – drescherjm May 03 '20 at 16:12
  • ***But isnt it bad practice to put everything in the header ?*** Not in the case of templates where having their implementation in the header is usually a requirement. – drescherjm May 03 '20 at 16:16

0 Answers0