C++ 为什么赢了';t std::shared_ptr接受我的删除函数对象?
我在*将deleter函子传递到std::smart\u ptr*时遇到问题。这是我第一次尝试这样的事情,所以我可能忽略了一些非常简单的事情 下面是我的函子类的样子C++ 为什么赢了';t std::shared_ptr接受我的删除函数对象?,c++,syntax,std,shared-ptr,functor,C++,Syntax,Std,Shared Ptr,Functor,我在*将deleter函子传递到std::smart\u ptr*时遇到问题。这是我第一次尝试这样的事情,所以我可能忽略了一些非常简单的事情 下面是我的函子类的样子 #pragma once; #ifndef ASSETDELETERS_H #define ASSETDELETERS_H #include "RenderSystem.h" struct SourceImageDeleter { RenderSystem & refGraphicsRenderer; u
#pragma once;
#ifndef ASSETDELETERS_H
#define ASSETDELETERS_H
#include "RenderSystem.h"
struct SourceImageDeleter
{
RenderSystem & refGraphicsRenderer;
unsigned int * ptrTextureID;
explicit SourceImageDeleter( RenderSystem & tempRef, unsigned int * tempPtrID )
: refGraphicsRenderer( tempRef ) ,
ptrTextureID(tempPtrID) {};
SourceImageDeleter( const SourceImageDeleter & originalCopy )
: refGraphicsRenderer( originalCopy.refGraphicsRenderer ) ,
ptrTextureID( originalCopy.ptrTextureID ) {};
void operator() ()
{
refGraphicsRenderer.deregisterTexture( ptrTextureID );
}
};
#endif
RenderSystem::deregisterTexture函数只需要一个参数(unsigned int*),因此,它在创建函子时传递。我已经研究了std::bind的使用,但是我没有太多的经验,也无法成功地使用它来代替函子
这是目前为止唯一使用它的方法
std::shared_ptr<SourceImage> Engine::createSourceImage( std::string tempFilepath )
{
SourceImage * tempImagePtr = new SourceImage( tempFilepath );
registerTexture( &tempImagePtr->textureID, &tempImagePtr->image );
return std::shared_ptr<SourceImage>( tempImagePtr , SourceImageDeleter( this->graphicsRenderer, &tempImagePtr->textureID ) );
}
std::shared_ptr Engine::createSourceImage(std::string tempFilepath)
{
SourceImage*tempImagePtr=新的SourceImage(tempFilepath);
registerTexture(&tempImagePtr->textureID,&tempImagePtr->image);
返回std::shared_ptr(tempmageptr,SourceImageDeleter(this->graphicsRenderer,&tempmageptr->textureID));
}
我不知道为什么它不起作用!我整个星期都在试图让我的smart_ptr运行一个自定义的删除函数,在试图弄清楚指向方法传递的指针是如何工作的、std::bind/std::mem_fun_ref是如何工作的以及函子是如何工作的之间,我整个星期都很困惑
无论如何,这里是Visual Studio给我的编译错误,我希望有人能帮助我找出我一直在搞砸的东西
error C2064: term does not evaluate to a function taking 1 arguments
1> class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(1438) : see reference to function template instantiation 'void std::tr1::shared_ptr<_Ty>::_Resetp<_Ux,_Dx>(_Ux *,_Dx)' being compiled
1> with
1> [
1> _Ty=SourceImage,
1> _Ux=SourceImage,
1> _Dx=SourceImageDeleter
1> ]
1> c:\projects\source\engine.cpp(151) : see reference to function template instantiation 'std::tr1::shared_ptr<_Ty>::shared_ptr<SourceImage,SourceImageDeleter>(_Ux *,_Dx)' being compiled
1> with
1> [
1> _Ty=SourceImage,
1> _Ux=SourceImage,
1> _Dx=SourceImageDeleter
1> ]
错误C2064:术语的计算结果不是采用1个参数的函数
1> 类未定义“运算符()”或用户定义的转换运算符,该运算符指向具有适当数量参数的函数指针或函数引用
1> c:\program files(x86)\microsoft visual studio 10.0\vc\include\memory(1438):请参阅正在编译的函数模板实例化“void std::tr1::shared\u ptr::\u Resetp(\u Ux*,\u Dx)”的参考
1> 与
1> [
1> _Ty=SourceImage,
1> _Ux=SourceImage,
1> \u Dx=源图像删除器
1> ]
1> c:\projects\source\engine.cpp(151):请参阅正在编译的函数模板实例化“std::tr1::shared_ptr::shared_ptr(_Ux*,_Dx)”
1> 与
1> [
1> _Ty=SourceImage,
1> _Ux=SourceImage,
1> \u Dx=源图像删除器
1> ]
(顺便说一句,engine.cpp(151)是上面显示的engine::createSourceImage内部的返回行。如果我删除deleter参数,除了与不正确的图像删除相关的明显的资源泄漏之外,程序编译和运行良好。
std::shared\u ptr
将被删除的指针传递到deleter,这正是错误消息所说的:该类没有定义具有正确参数数的运算符()
您的删除程序不需要任何参数,因此无法工作;您需要将其更改为
void operator()(SourceImage*)
好吧,我知道这会很简单。。我现在整个脑袋都在掌纹!它很有魅力。。我会尽快接受答案。。谢谢你的帮助!