Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 类方法模板参数列表_C++_Templates - Fatal编程技术网

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()); }