2

Well, the title says everything. I get this message when linking(compilation is successful):

F:/Android/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/ ../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.ex e: ./obj/local/armeabi/objs/hellondk/Player/Renderer.o: in function Renderer::On SurfaceCreated():jni/Player/Renderer.cpp:63: error: undefined reference to 'void GameObject::test<void>()'

GameObject.h:

class GameObject
{
    public:
    ...
    template<typename T> T test();
}

GameObject.cpp:

template<typename T> T GameObject::test()
{

}

Renderer.cpp(using the template):

object = new GameObject();
...
object->test<void>(); // error here
Gintas_
  • 4,940
  • 12
  • 44
  • 87

2 Answers2

4

You must define template functions in a header file, otherwise the compiler won't know where to find the definition and only the declaration, which gives you a linker error like the one you have.

pippin1289
  • 4,861
  • 2
  • 22
  • 37
  • 2
    Here's a nice little page on linking with templates http://www.parashift.com/c++-faq-lite/separate-template-fn-defn-from-decl.html It basically goes to describe two different ways of solving it, one of which is the solution above – Lochemage Aug 19 '13 at 17:00
0

You declare your template as test() but you are trying to call a function called testas(). This may be your problem. Where is testas() defined?

Lochemage
  • 3,974
  • 11
  • 11
  • sorry, copy-paste mistake, fixed. – Gintas_ Aug 19 '13 at 16:40
  • Ah, I see the edit, but your error message at the top still states `testas()`. Is that also a paste error? – Lochemage Aug 19 '13 at 16:42
  • Yes, sorry about that :D. Fixed again... – Gintas_ Aug 19 '13 at 16:44
  • Ok, the error message also states that your call to `test()` function is trying to pass in a `ComponentType` as a parameter, but your definition has no parameters. I've tried your snippet of code on my compiler and it works fine, there must be something else involved that you may have left out. – Lochemage Aug 19 '13 at 16:46
  • Fixed once again... Moved it to header file and now it works fine. – Gintas_ Aug 19 '13 at 16:52