C++ 类方法模板参数列表
我在接口中声明了以下模板方法:C++ 类方法模板参数列表,c++,templates,C++,Templates,我在接口中声明了以下模板方法: class IObjectFactory { public: virtual ~IObjectFactory() { } virtual int32_t Init() = 0; virtual bool Destroy() = 0; virtual bool Start() = 0; virtual bool Stop() = 0; virtual bool isRunning() = 0; virtual
class IObjectFactory
{
public:
virtual ~IObjectFactory() { }
virtual int32_t Init() = 0;
virtual bool Destroy() = 0;
virtual bool Start() = 0;
virtual bool Stop() = 0;
virtual bool isRunning() = 0;
virtual void Tick() = 0;
template <class T>
Object<T> CreateObject(T);
};
我得到的错误是“没有函数模板的实例与参数列表匹配”
正确的函数调用是什么样子的
(另请注意,在接口中声明模板方法并要求用户实现它可以吗?因为您不能将其声明为虚拟的)
谢谢您得到了这个错误,因为您没有将
TestClass1
的对象传递给这个方法
正确编译代码如下所示:
inline void AllocateWithMemPoolAux()
{
TestClass1 tObj;
mObjFactory->CreateObject<TestClass1>(tObj);
}
inline void allocateWithMempoaux()
{
试验1-tObj;
mObjFactory->CreateObject(tObj);
}
我假设您的分配thmethmoolaux()
的副作用比这里显示的要多;至少,您需要对CreateObject
函数的返回值执行一些操作(更好的设计是通过引用将对象传递到此函数中)。模板函数
template <class T>
Object<T> CreateObject(T);
模板
对象CreateObject(T);
接受一个类型为T
的参数。您在没有它的情况下调用了它。注意:
template <class T>
Object<T> CreateObject(T);
模板
对象CreateObject(T);
你是说
template <class T>
Object<T> CreateObject();
模板
对象CreateObject();
?您在没有参数的情况下调用了该方法:
mObjFactory->CreateObject<TestClass1>();
mObjFactory->CreateObject();
您需要传递TestClass1类型的对象:
inline void AllocateWithMemPoolAux() { mObjFactory->CreateObject<TestClass1>(TestClass1()); }
inline void AllocateWithMemPoolAux(){mObjFactory->CreateObject(TestClass1());}
inline void AllocateWithMemPoolAux() { mObjFactory->CreateObject<TestClass1>(TestClass1()); }