Visual Studio 2017 C2027 未定义类型的使用'SDL_Texture'
Visual Studio 2017 C2027 use of undefined type 'SDL_Texture'
我从在 gcc 中编译我的代码转向 Visual Studio 2017 提供的编译器。
每次我尝试 运行 应用程序时,我都会收到以下错误:
这是我认为错误来源的文件:
Texture.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
Texture.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。
SDL.h不包含SDL_Texture
的定义。它只包含一个前向声明。这是因为您永远不会直接使用该类型,而只会使用指向它的指针。但是,为了实例化 std::unique_ptr<SDL_Texture>
,您需要完整的定义。
但是你为什么在这里使用没有自定义删除器的 std::unique_ptr
,因为你只需要用 SDL_DestroyTexture
手动删除对象?这违背了使用智能指针的全部目的。试试这个:
struct TextureDeleter
{
void operator()(SDL_Texture* tp) {
SDL_DestroyTexture(tp);
}
};
std::unique_ptr<SDL_Texture, TextureDeleter> m_texture;
SDL_Texture
结构未在 SDL.h 中声明。它是一个内部类型,只有它的前向声明在 public 接口中可用,你不能 delete
它。您应该将自定义删除器提供给智能指针 std::unique_ptr<SDL_Texture, void(SDL_Texture*)> m_texture
并稍后分配 m_texture = std::unique_ptr(SDL_CreateTextureFromSurface(...), SDL_DestroyTexture)
.
我从在 gcc 中编译我的代码转向 Visual Studio 2017 提供的编译器。
每次我尝试 运行 应用程序时,我都会收到以下错误:
这是我认为错误来源的文件:
Texture.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
Texture.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。
SDL.h不包含SDL_Texture
的定义。它只包含一个前向声明。这是因为您永远不会直接使用该类型,而只会使用指向它的指针。但是,为了实例化 std::unique_ptr<SDL_Texture>
,您需要完整的定义。
但是你为什么在这里使用没有自定义删除器的 std::unique_ptr
,因为你只需要用 SDL_DestroyTexture
手动删除对象?这违背了使用智能指针的全部目的。试试这个:
struct TextureDeleter
{
void operator()(SDL_Texture* tp) {
SDL_DestroyTexture(tp);
}
};
std::unique_ptr<SDL_Texture, TextureDeleter> m_texture;
SDL_Texture
结构未在 SDL.h 中声明。它是一个内部类型,只有它的前向声明在 public 接口中可用,你不能 delete
它。您应该将自定义删除器提供给智能指针 std::unique_ptr<SDL_Texture, void(SDL_Texture*)> m_texture
并稍后分配 m_texture = std::unique_ptr(SDL_CreateTextureFromSurface(...), SDL_DestroyTexture)
.