首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么std::shared_ptr不接受我的删除函数对象?

为什么std::shared_ptr不接受我的删除函数对象?
EN

Stack Overflow用户
提问于 2013-11-08 02:02:03
回答 1查看 478关注 0票数 0

我对*将删除函子传递到std::smart_ptr*有问题。这是我第一次尝试这样的方法,所以我可能忽略了一些非常简单的东西。

这是我的函数类的样子;

代码语言:javascript
复制
#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函数只需要一个参数(无符号int *),因此它在函数创建时被传递。我已经研究了std::bind的使用,但是我没有太多的经验,也无法成功地使用它来代替函子。

到目前为止,这是唯一使用它的方法。

代码语言:javascript
复制
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给我的编译错误,我希望有人能帮我弄清楚我搞砸了什么;

代码语言:javascript
复制
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,如上所示。如果我删除了删除参数,除了与不正确的图像删除相关的明显资源泄漏外,程序还可以编译和运行。

EN

Stack Overflow用户

回答已采纳

发布于 2013-11-08 02:06:18

std::shared_ptr将被删除的指针传递给删除器,这正是您的错误消息所说的:类没有定义具有正确的参数数的运算符()。

删除器不需要任何参数,因此无法工作;您需要将其更改为void operator()(SourceImage*)

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19850314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档