Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Define template member properly

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
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 
ModulFuncLoader.cpp
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
} 
But do I must to do it for every type or I can do some less complicated way?
You can simply put your template definitions in the header file instead of your source file. Otherwise you need to explicitly define them.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by SGUdyrNov 16, 2016 12:39 PM 19 343 Nov 16, 2016 12:39 PM
Last Post: Sajuuk
[C] by TicomaFeb 12, 2016 01:34 PM 3 132 Feb 12, 2016 01:34 PM
Last Post: Annabelle Lee
[C] by iScienceNov 8, 2015 01:26 AM 0 93 Nov 8, 2015 01:26 AM
Last Post: iScience