我有一个非模板抽象基类,用于为了能够引用基类型,因为未专用的模板类型不能用作方法参数。C++ HowTo:从抽象专用模板继承的非模板类
#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
抽象模板类ICallback继承ICallback碱,像这样:
#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
最后,可以通过从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
所有这些都编译好,但是当我尝试使用这个,像这样:
rpcpp::SampleCallback sc;
sic.CalculateMean(15, 28, &sc); // Third argument of this method is expected to be ICallbackBase&.
它产生以下两个错误:
“can not instantiate abstract class”in line#1。 符合#2
“不能从SampleCallback &转换参数3 ICallbackBase &”我在做什么错?
你说这需要一个参考,但你传递一个指针。 – 2012-03-11 17:26:05