我对*将删除函子传递到std::smart_ptr*有问题。这是我第一次尝试这样的方法,所以我可能忽略了一些非常简单的东西。
这是我的函数类的样子;
#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 );
}
};
#endifRenderSystem::deregisterTexture函数只需要一个参数(无符号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 ) );
}我不知道为什么不行!我基本上一直在尝试让我的smart_ptr运行一个定制的删除函数一周,在试图找出指针到方法传递的工作原理、std::bind/std::mem_smart_ptr_ref是如何工作,以及函子的工作是如何阻碍我整个星期。
不管怎么说,这是Visual给我的编译错误,我希望有人能帮我弄清楚我搞砸了什么;
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> ](顺便说一句,engine.cpp(151)是引擎内部的返回线::createSourceImage,如上所示。如果我删除了删除参数,除了与不正确的图像删除相关的明显资源泄漏外,程序还可以编译和运行。
发布于 2013-11-08 02:06:18
std::shared_ptr将被删除的指针传递给删除器,这正是您的错误消息所说的:类没有定义具有正确的参数数的运算符()。
删除器不需要任何参数,因此无法工作;您需要将其更改为void operator()(SourceImage*)
https://stackoverflow.com/questions/19850314
复制相似问题