Possible Duplicate:
C++ template, linking error
I have two linking errors and I have no idea what's wrong with the code and how to fix them:
main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall A::A(void)" (??0?$A@VB@@@@QAE@XZ) referenced in function "public: __thiscall B::B(void)" (??0B@@QAE@XZ)
and
main.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall A::exec(void (__thiscall B::*)(void))" (?exec@?$A@VB@@@@QAEXP8B@@AEXXZ@Z) referenced in function "public: void __thiscall B::run(void)" (?run@B@@QAEXXZ)
Explaining the code a little:
This class has to execute a function from the derived class. function exec is called from the derived class with a function from the derived class parameter. Signature of this function is void function();
//header.h
#ifndef HEADER_H
#define HEADER_H
template <class T>
class A
{
public:
typedef void (T::*ExtFunc)();
A();
void funcA();
void exec(ExtFunc func);
};
#endif // HEADER_H
//header.cpp
#include "header.h"
template<typename T>
A<T>::A() { }
template<typename T>
void A<T>::funcA()
{
cout << "testA\n";
}
template<typename T>
void A<T>::exec(ExtFunc func)
{
(T().*func)();
}
In main.cpp
I derive a class from A class and pass the derived class as template paramtere. Then I execute function exec
through the run()
function.
//main.cpp
#include <iostream>
#include "header.h"
using namespace std;
class B : public A<B>
{
public:
B() { }
void run()
{
exec(&B::funcB);
}
void funcB()
{
cout << "testB\n";
}
};
int main()
{
B ob;
ob.run();
return 0;
}
Can anyone tell me what's going on?...