我得到奇怪的错误,试图通过自定义deletor到std :: shared_ptr的: - 在有任何东西做与它的情况下>unsigned char*
的std :: shared_ptr的Deletor类型
std::shared_ptr<unsigned char*> SDLWindow::drawGrid(const Grid* grid) {
SDL_Surface* rgbSurface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGB888, 0);
//...error checking and locking the SDL_Surface, omitted for brevity
unsigned char* pixelsPtr = (unsigned char*)(rgbSurface->pixels);
//need a custom deleter because we created a copy of the SDL_Surface
//we cant directly delete the pixel data but need to delete the underlying SDL_Surface instead
auto surfaceDeleter = [rgbSurface](decltype(pixelsPtr)* ignored)
//don't directly delete the pixel buffer, delete the underlying SDL_Surface instead
{
//unlock the surface if necessary
if(SDL_MUSTLOCK(rgbSurface))
{
SDL_UnlockSurface(rgbSurface);
}
SDL_FreeSurface(rgbSurface);
};
return std::shared_ptr<unsigned char*>(pixelsPtr, surfaceDeleter);
}
(我改变const unsigned char*
但我宁愿尽可能使用常量)
锵说,有关不能够转化为nullptr_t
,我以为是关系到shared_ptrs如何使用类型擦除(但为什么不应该他们能够处理的指针? )
unsigned char* pixelsPtr = (unsigned char*)(rgbSurface->pixels);
[..]/src/SDLWindow.cpp:132:12: error: no matching constructor for initialization of 'std::shared_ptr<unsigned char *>'
return std::shared_ptr<unsigned char*>(pixelsPtr, surfaceDeleter);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:3809:26: note: candidate constructor [with _Dp = (lambda at
/[..]/src/SDLWindow.cpp:122:27)] not viable: no known conversion from 'unsigned char *' to 'nullptr_t' for 1st argument
template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
奇怪的是G ++似乎给了一个完全不同的错误:
from /[..]/src/SDLWindow.hpp:4,
from /[..]/src/SDLWindow.cpp:1:
/opt/local/include/gcc49/c++/bits/shared_ptr_base.h: In instantiation of 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*, _Deleter) [with _Tp1 = unsigned char; _Deleter = jakway_antf::SDLWindow::drawGrid(const jakway_antf::Grid*)::<lambda(unsigned char**)>; _Tp = unsigned char*; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]':
/opt/local/include/gcc49/c++/bits/shared_ptr.h:130:37: required from 'std::shared_ptr<_Tp>::shared_ptr(_Tp1*, _Deleter) [with _Tp1 = unsigned char; _Deleter = jakway_antf::SDLWindow::drawGrid(const jakway_antf::Grid*)::<lambda(unsigned char**)>; _Tp = unsigned char*]'
/[..]/src/SDLWindow.cpp:132:69: required from here
/opt/local/include/gcc49/c++/bits/shared_ptr_base.h:881:37: error: cannot convert 'unsigned char*' to 'unsigned char**' in initialization
: _M_ptr(__p), _M_refcount(__p, ntf::SDLWindow::drawGrid(const jakway_antf::))
至于G ++我不知道发生了什么事,因为我受够了这种参数的拉姆达混乱,并宣布它unsigned char**
没有按”不要做任何事情。
我已经试过:
- 与捕获参数,它似乎并没有做任何事情(从我从几个小时前记得了)
- 铸造每哪种方式
- 搞乱用std :: function声明lambda而不是自动的 ,我相当坚持。
我相信'std :: shared_ptr'应该是'std :: shared_ptr ' –
NathanOliver
@NathanOliver Yikes,我真的应该抓住这个。谢谢! – Prime