Ошибка компоновщика при реализации шаблона проектирования адаптера

Я получаю ошибку компоновщика в коде ниже.
Если я сделаю функцию ClientAPI () ClientInterface чисто виртуальной, ошибка компоновщика исчезнет.
В чем причина такого поведения?

    // How the interface looks like to the Client
class ClientInterface
{
public:
virtual void ClientAPI();
virtual ~ClientInterface(){}
};

template <class TYPE>   //this adaptor class can adapt to any type of legacy application as it is a generic function that uses template parameter to point to any legacy application
class Adaptor : public ClientInterface
{
public:
Adaptor(TYPE *objPtr, void (TYPE:: *fnPtr)())
{
m_objPtr = objPtr;
m_fnPtr = fnPtr;
}
void ClientAPI()
{
/*....
Do the conversion logic reqd to convert the user params into the params expected by your original legacy application...
....*/
(m_objPtr->*m_fnPtr)(); //Would call the method of the legacy application internally
}
~Adaptor()
{
if(m_objPtr)
delete m_objPtr;
}
private:
TYPE *m_objPtr; //You can keep either pointer to the Legacy implementation or derive the Legacy implementation privately by your Adaptor class
void (TYPE:: *m_fnPtr)();

};

//Adaptee classes below..
class LegacyApp1
{
public:
void DoThis()
{
cout<<"Adaptee1 API"<<endl;
}
};

// Класс исполнения, в котором определен main и у меня есть «Adaptor.h»

#include "headers.h"#include "Adaptor.h"
void Adapter()
{
ClientInterface **interface_ptr = new ClientInterface *[2];
interface_ptr[0] = new Adaptor<LegacyApp1>(new LegacyApp1() , &LegacyApp1::DoThis);
interface_ptr[1] = new Adaptor<LegacyApp2>(new LegacyApp2() , &LegacyApp2::DoThat);
for(int i = 0; i < 2 ; i++)
{
interface_ptr[i]->ClientAPI();
}
}

int main()
{
//Testing();
Adapter();

char ch;
cin>>ch;
return 0;
}

0

Решение

Таким образом, исправление в приведенном выше коде выглядит следующим образом: Просто внесите следующие изменения в первые несколько строк исходного кода.

// How the interface looks like to the Client
class ClientInterface
{
public:
//earlier I forgot to define it, so it was giving linker error as  the function is just declared but not defined.
virtual void ClientAPI(){}
virtual ~ClientInterface(){}
};

Благодарю.

0

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector