0

I'm trying to call my templated function with no success :(

My memaddr.h:

#ifndef __MEM_ADDR_WRITER__
#define __MEM_ADDR_WRITER__

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

namespace MemAddr {

    template <typename WRITABLE>
    int WriteMemoryAddress(const char* process, unsigned long address, WRITABLE value);

}

#endif

The call:

byte value = 0xEB;
MemAddr::WriteMemoryAddress("jamp.exe", 0x004392D2, value);

Error Message:

undefined reference to `int MemAddr::WriteMemoryAddress<unsigned char>(char const*, unsigned long, unsigned char)`

Answer: Templates needs to be defined in headers.. (@Shaggi)

Iburidu
  • 450
  • 2
  • 5
  • 15
  • 1
    Put the definition in the header file (templates needs this) – Shaggi Oct 13 '13 at 18:27
  • You're using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Oct 13 '13 at 18:27
  • Thanks @Shaggi, this was the right answer. But it is really stupid. My function needs another function from the header file (which does not use template), but i needed to define that too in header. So If I use templates, I must forget headers almost.. – Iburidu Oct 13 '13 at 18:50
  • possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Alan Stokes Oct 13 '13 at 19:17
  • 1
    Yeah its kinda weird but makes sense when you think about it (the compiler generates a new function for each different template parameter, it needs the source for this). You shouldn't have a problem with non-templates though? – Shaggi Oct 13 '13 at 21:48

1 Answers1

0

The function template

template <typename WRITABLE>
int WriteMemoryAddress(const char* process, unsigned long address, WRITABLE value);

is not defined. Include its definition in the header file (memaddr.h):

template <typename WRITABLE>
int WriteMemoryAddress(const char* process, unsigned long address, WRITABLE value)
{
    /* do something */
}

The function template will be instantiated once when you use it.

Ron Lau
  • 159
  • 1
  • 10
  • please be more specific, template is built-in in c++ http://www.cplusplus.com/doc/tutorial/templates/ – Iburidu Oct 13 '13 at 18:43