I am trying to invoke a method of one class in a method of another class. The following code is working, but when I define the Invoke method of class Second in a separate .cpp file and not inline like it is now, I get the following error: undefined reference to 'void Second::Invoke(First*, TMethodDelegate::OneParam::TMethodPtr_Const)'. Can anyone explain it to me why the Invoke method works only when it is defined inline?
// MethodDelegate.h
#pragma once
template <class TClass>
class TMethodDelegate
{
public:
/** TMethodDelegate with no parameters */
template <typename TRetValue>
class NoParams
{
public:
typedef TRetValue(TClass::*TMethodPtr)();
typedef TRetValue(TClass::*TMethodPtr_Const)() const;
private:
inline NoParams() {}
};
/** TMethodDelegate with one parameter */
template <typename TRetValue, typename TParam0>
class OneParam
{
public:
typedef TRetValue(TClass::*TMethodPtr)(TParam0);
typedef TRetValue(TClass::*TMethodPtr_Const)(TParam0) const;
private:
inline OneParam() {};
};
private:
inline TMethodDelegate() {}
};
// First.h
#pragma once
#include <iostream>
using std::cout;
using std::endl;
class First
{
public:
inline void PrintInt(int number) const
{
cout << number << endl;
}
};
// Second.h
#pragma once
#include "MethodDelegate.h"
class Second
{
public:
// When defined in a separate .cpp file I get an undefined error when I am trying to call the method in the main function
template <class TClass>
inline void Invoke(TClass* object, typename TMethodDelegate<TClass>::template OneParam<void, int>::TMethodPtr_Const Func)
{
if (object != 0 && Func != 0)
{
int number = 10;
(object->*Func)(number);
}
}
};
// main.cpp
#include "First.h"
#include "Second.h"
int main()
{
First first;
Second second;
second.Invoke(&first, &First::PrintInt); // I get the error at this line. If I comment the line - the code is compiling. This is only if the Invoke method is defined in a separate .cpp file of course
}