Sep 24, 2015 10:41 AM
(This post was last modified: Sep 24, 2015 10:56 AM by zNinja.)
Hello, I have a class which has template function members.
When I define body of template member in class declaration all is ok at compile time,
but if I want to declare template member outside the class in cpp file i get error at compile time.
"undefined reference to `void* ModulFuncLoader::execute<void*>(void*, char const*, void*)'|"
ModulFuncLoader.h
ModulFuncLoader.cpp
main.cpp
I can declare for single type like this in cpp file
But do I must to do it for every type or I can do some less complicated way?
When I define body of template member in class declaration all is ok at compile time,
but if I want to declare template member outside the class in cpp file i get error at compile time.
"undefined reference to `void* ModulFuncLoader::execute<void*>(void*, char const*, void*)'|"
ModulFuncLoader.h
PHP Code
<?php
#ifndef MODULFUNCLOADER_H
#define MODULFUNCLOADER_H
#include <windows.h>
#include <vector>
typedef void* (*_main0)();
typedef void* (*_main1)(void*);
typedef void* (*_main2)(void*,void*);
typedef void* (*_main3)(void*,void*,void*);
typedef void* (*_main4)(void*,void*,void*,void*);
typedef void* (*_main5)(void*,void*,void*,void*,void*);
typedef void* (*_main6)(void*,void*,void*,void*,void*,void*);
typedef void* (*_main7)(void*,void*,void*,void*,void*,void*,void*);
#define mn(n) _main##n
class ModulFuncLoader
{
public:
ModulFuncLoader(const char* lib_path);
virtual ~ModulFuncLoader();
template<class T>
T execute(T t,const char* func){
//if(!lib) throw exception
mn(0) __main = (mn(0))GetProcAddress(lib,func);
if(__main)
return __main();
return NULL;// should throw exception instead
}
template<class T>
T execute(T t,const char* func,void*);
protected:
private:
HMODULE lib;
};
#endif // MODULFUNCLOADER_H
PHP Code
<?php
#include "ModulFuncLoader.h"
ModulFuncLoader::ModulFuncLoader(const char* lib_path)
{
lib = LoadLibrary(lib_path);
}
ModulFuncLoader::~ModulFuncLoader()
{
if(lib)
FreeLibrary(lib);
}
template<class T>
T ModulFuncLoader::execute(T t,const char* func, void*p1)
{
//if(!lib) throw exception
mn(1) __main = (mn(0))GetProcAddress(lib,func);
if(__main)
return __main(p1);
return NULL;// should throw exception instead
}
main.cpp
PHP Code
<?php
#include "ModulFuncLoader.h"
int main()
{
ModulFuncLoader m("c512.dll");
void* t = NULL;
m.execute(t,"alert"); // can compile
m.execute(t,"alert",(void*)"test"); // can not compile
return 0;
}
I can declare for single type like this in cpp file
PHP Code
<?php
template<>
int ModulFuncLoader::execute(int t,const char* func, void* p1){
// code
}


