首页 > 解决方案 > 如何使用 std::make_unique() 在构造函数中启动 std::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),我添加了导致第一个错误的行。

标签: c++sdlunique-ptr

解决方案


推荐阅读