如何使用std :: make_unique()

时间:2018-06-20 17:58:56

标签: c++ sdl unique-ptr

更新 我读了另一篇文章,发现自己可以写: cGraphics() : m_Window(nullptr, SDL_DestroyWindow), m_Renderer(nullptr, SDL_DestroyRenderer) {},可消除第一个错误。但是它仍然显示第二个错误:

  

错误C2512:“ std :: unique_ptr :: unique_ptr”:没有适当的默认构造函数可用

更新结束

我正在尝试将智能指针与SDL2一起使用,到目前为止,我在语法方面苦苦挣扎:

#pragma once

#include <memory>
#include <unordered_map>
#include <string>
#include "SDL.h"
#include "SDL_image.h"

struct cRGB
{
    int r, g, b;
};

class cGraphics
{
public:
    //  Creator functions for initialization
    bool Create_Window(int xWin, int yWin);
    bool Create_Renderer();
    bool Create_Texture(std::string texid, std::string texres, int r, int g, int b);

    //  ctor & dtor
    cGraphics() : m_Window(std::make_unique<SDL_Window>(nullptr, [](SDL_Window* w) {delete w; })), m_Renderer(std::make_unique<SDL_Renderer>(nullptr, [](SDL_Renderer* r) {delete r; })) {}
    cGraphics(int xWin, int yWin);
    ~cGraphics();

    //  Rendering
    void ClearScreen();
    void RenderTexture(std::string texres, int xDes, int yDes);
private:
    m_Renderer(nullptr, SDL_DestroyRenderer) {}
    std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> m_Window;
    std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)> m_Renderer;
    std::unordered_map<std::string, std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)>> m_Texture;

    //  Creator helper
    SDL_Texture* CreateTextureRawPtr(std::string texres, int r, int g, int b);
};

cGraphics::cGraphics(int xWin, int yWin)
{
    if (!Create_Window(xWin, yWin) || !Create_Renderer())
    {

    }
}

cGraphics::~cGraphics()
{
    IMG_Quit();
    SDL_Quit();
}

void cGraphics::ClearScreen()
{
    SDL_RenderClear(m_Renderer.get());
}

void cGraphics::RenderTexture(std::string texres, int xDes, int yDes)
{
    SDL_Rect g_SrcRect = { 0, 0, 32, 32 };          //  hard-coded for test
    SDL_Rect g_DesRect = { xDes, yDes, 32, 32 };
    SDL_RenderCopy(m_Renderer.get(), m_Texture[texres].get(), &g_SrcRect, &g_DesRect);
}

bool cGraphics::Create_Window(int xWin, int yWin)
{
    m_Window.reset(SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, xWin, yWin, SDL_WINDOW_SHOWN));
    return true;
}

bool cGraphics::Create_Renderer()
{
    m_Renderer.reset(SDL_CreateRenderer(m_Window.get(), -1, 0));
    SDL_SetRenderDrawColor(m_Renderer.get(), 255, 255, 255, 0xff);
    return true;
}

bool cGraphics::Create_Texture(std::string texid, std::string texres, int r, int g, int b)
{
    m_Texture.emplace(texid, std::make_unique<SDL_Texture>(CreateTextureRawPtr(texres, r, g, b)));
    return true;
}

SDL_Texture* cGraphics::CreateTextureRawPtr(std::string texres, int r, int g, int b)
{
    SDL_Surface* temp = IMG_Load(texres.c_str());
    //Set color key
    SDL_SetColorKey(temp, SDL_TRUE,
        SDL_MapRGB(temp->format, r, g, b));

    SDL_Texture* pTexture = SDL_CreateTextureFromSurface(m_Renderer.get(), temp);

    SDL_FreeSurface(temp);
    return pTexture;
}

我遇到两个错误:

该行的第一个(我添加此行是因为它可能会解决第二个错误

cGraphics() : m_Window(std::make_unique<SDL_Window>(nullptr, [](SDL_Window* w) {delete w; })), m_Renderer(std::make_unique<SDL_Renderer>(nullptr, [](SDL_Renderer* r) {delete r; })) {}

它抱怨没有构造函数的实例与参数匹配。基本上,我试图使用默认的删除器初始化它们,并使用reset放置一个真正的删除器SDL_DestroyWindow()

此错误导致连锁反应,几乎每行都包含一个错误。因此,我假设一旦解决此问题,其他大多数错误就会消失。

第二个错误(即,如果我删除导致第一个错误的行):

cGraphics::cGraphics(int xWin, int yWin)
{
    if (!Create_Window(xWin, yWin) || !Create_Renderer())
    {

    }
}

我没有完成内部代码块,但已经在抱怨

  

std :: unique_ptr类没有默认的ctor

我不确定如何从这里继续,甚至谷歌搜索显示此特定错误的结果为零。所以我认为可能是因为没有默认的ctor(我实际上想将默认的ctor放在private),并且我添加了导致第一个错误的行。

0 个答案:

没有答案