【问题标题】:Visual Studio 2017 C2027 use of undefined type 'SDL_Texture'Visual Studio 2017 C2027 使用未定义类型“SDL_Texture”
【发布时间】:2018-03-07 17:51:47
【问题描述】:

我从在 gcc 中编译我的代码转移到 Visual Studio 2017 提供的编译器。

每次我尝试运行应用程序时都会收到以下错误:

这是我认为错误来自的文件:

纹理.h

#include <SDL2/SDL.h>
#include <memory>
#include <string>
namespace AcsGameEngine {

class Renderer;

class Texture {
public:
    Texture(const Renderer& renderer);
    Texture(const Renderer& renderer, const std::string&);
    Texture(const Texture& orig) = default;
    virtual ~Texture();

    void load(const std::string&, uint16_t w = 0, uint16_t h = 0) const;
    void load(const char*, uint16_t w = 0, uint16_t h = 0);

    const Renderer& getRenderer() const { return m_renderer; }

    //inline SDL_Texture* getRawPointer() const { return m_texture->get(); }

private:
    std::unique_ptr<SDL_Texture> m_texture;
    const Renderer& m_renderer;

    uint16_t m_width;
    uint16_t m_height;
};
} // namespace AcsGameEngine

纹理.cpp

#include "Texture.h"
#include "Renderer.h"
#include <SDL2/SDL_image.h>

namespace AcsGameEngine {

    Texture::Texture(const Renderer &renderer) : m_renderer(renderer) {
    }

    Texture::Texture(const Renderer &renderer, const std::string &p) : Texture(renderer) {
        load(p.c_str());
    }

    Texture::~Texture() {
        if (m_texture != nullptr) {
            SDL_DestroyTexture(m_texture.get());
        }
    }

    void Texture::load(const std::string &path, uint16_t w, uint16_t h) const {
        load(path.c_str());
    }

    void Texture::load(const char *path, uint16_t w, uint16_t h) {
        SDL_Surface *tmp = IMG_Load(path);
        m_texture.reset(SDL_CreateTextureFromSurface(m_renderer.getRawPointer(), tmp));
        SDL_FreeSurface(tmp);

        if (m_texture == false) {
            //error
        }

    }

} // namespace AcsGameEngine

我不明白为什么它抱怨未定义类型,因为 SDL.h 包含结构 SDL_Texture。

【问题讨论】:

    标签: c++ visual-studio-2017 sdl-2


    【解决方案1】:

    SDL.h 不包含SDL_Texture 的定义。它只包含它的前向声明。这是因为您永远不会直接使用该类型,您只能使用指向它的指针。但是,为了实例化std::unique_ptr&lt;SDL_Texture&gt;,您需要完整的定义。

    但是为什么你在这里使用没有自定义删除器的std::unique_ptr,因为你只需要手动删除带有SDL_DestroyTexture 的对象?这违背了使用智能指针的全部目的。试试这个:

    struct TextureDeleter
    {
        void operator()(SDL_Texture* tp) {
            SDL_DestroyTexture(tp);
        }
    };
    
    std::unique_ptr<SDL_Texture, TextureDeleter> m_texture;
    

    【讨论】:

    • 谢谢。我想我对此感到困惑,因为这似乎也不能用 gcc 正确编译。
    【解决方案2】:

    SDL_Texture 结构未在 SDL.h 中声明。它是一个内部类型,只有它的前向声明在公共接口中可用,你不能delete它。您应该为智能指针std::unique_ptr&lt;SDL_Texture, void(SDL_Texture*)&gt; m_texture 提供自定义删除器并稍后分配m_texture = std::unique_ptr(SDL_CreateTextureFromSurface(...), SDL_DestroyTexture)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      相关资源
      最近更新 更多