I have a non-template abstract base class which is used in order to be able to have a reference to the base type, since unspecialized template types can’t be used as method arguments.
#ifndef RPCPP_ICALLBACKBASE_H
#define RPCPP_ICALLBACKBASE_H
#include <string>
namespace rpcpp
{
class ICallbackBase
{
public:
virtual ~ICallbackBase() {};
virtual void OnSuccess(void result) = 0;
virtual void OnError(std::string error) = 0;
};
}
#endif // RPCPP_ICALLBACKBASE_H
The abstract template class ICallback inherits from ICallback base, like so:
#ifndef RPCPP_ICALLBACK_H
#define RPCPP_ICALLBACK_H
#include "ICallbackBase.h"
namespace rpcpp
{
template <class T>
class ICallback : public ICallbackBase
{
public:
virtual ~ICallback() {};
virtual void OnSuccess(T result) = 0;
virtual void OnError(std::string error) = 0;
};
}
#endif // RPCPP_ICALLBACK_H
Finally, one can create a concrete type by inheriting from ICallback:
#ifndef RPCPP_SAMPLE_CALLBACK_H
#define RPCPP_SAMPLE_CALLBACK_H
#include "ICallback.h"
#include <iostream>
namespace rpcpp
{
class SampleCallback : public ICallback<double>
{
public:
~SampleCallback() {};
virtual void OnSuccess(double result)
{
std::cout << "Successfully executed a remote procedure, A + B = " << result << "\r\n\r\n";
}
virtual void OnError(std::string error)
{
std::cout << "Error while executing a remote procedure: " << error << "\r\n\r\n";
}
};
}
#endif // RPCPP_SAMPLE_CALLBACK_H
All of which compiles nice, however when i try to use this, like so:
rpcpp::SampleCallback sc;
sic.CalculateMean(15, 28, &sc); // Third argument of this method is expected to be ICallbackBase&.
It produces the following two errors:
“cannot instantiate abstract class” in line #1.
“cannot convert parameter 3 from SampleCallback& to ICallbackBase&” in line #2
What am I doing wrong?
is never defined.