0

I'm working on an open-source project: https://github.com/daniftodi/metal

I have ArrayHolder class defined in holders/ArrayHolder.h and implemented in holders/ArrayHolder.cpp

My code compiles; but in my main.cpp; I need to:

#include "holders/ArrayHolder.cpp"

If I don't do that; I get this error:

main.cpp.o:(.rodata._ZTV11ArrayHolderI8FunctionE[_ZTV11ArrayHolderI8FunctionE]+0x10): undefined reference to `ArrayHolder<Function>::put(Function*)

I don't understand problem and can't find a solution. Please help me.

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
0x3d
  • 460
  • 1
  • 8
  • 27
  • 1
    Include the header file in main.cpp and when linking, tell compiler to link your cpp file – matesc Feb 13 '17 at 11:47
  • 1
    Can you go a little more into detail what your problem is? Maybe some CMake code? Because "If I don't include ArrayHolder.cpp ... I get an linker error" seems pretty obvious. Just a guess: you are building a library and forgot to link it to `main` with the [`target_link_libraries()`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) command? – Florian Feb 13 '17 at 11:48
  • Here is my CMakeList file: https://github.com/daniftodi/metal/blob/master/CMakeLists.txt – 0x3d Feb 13 '17 at 13:19

1 Answers1

1

If you work with templates; you need to define them in the header file; you cannot define them in the source files like you would do with normal methods or functions.

See this question for further reference: Why can templates only be implemented in the header file?

Community
  • 1
  • 1
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • Thanks. I found also a useful explanation here: https://www.codeproject.com/Articles/48575/How-to-define-a-template-class-in-a-h-file-and-imp – 0x3d Feb 13 '17 at 13:23