I'm making some SDL2 wrappers in C++. Like this:
/* header file */
#include <SDL_mixer.h>
#include <memory>
class SDL2_Music {
public:
~SDL2_Music() { free(); }
bool loadMusic(const std::string& path);
bool play(int loops = -1);
// more methods
private:
void free();
Mix_Music* music_ = nullptr;
};
/* cpp file */
void SDL2_Music::free() {
if (music_ != nullptr) {
Mix_FreeMusic(music_);
music_ = nullptr;
}
}
bool SDL2_Music::loadMusic(const std::string& path) {
free();
music_ = Mix_LoadMUS(path.c_str()); // this returns a Mix_Music*
if (music_ == nullptr) {
ktp::logSDLError("Mix_LoadMUS");
return false;
}
return true;
}
// more stuff
This works fine, but I want to get rid of the raw pointer, so I can also get rid of the free()
method and the dtor
invoking it (yes, I'm reading about the rule of 0). So I made the following changes:
/* header file */
#include <SDL_mixer.h>
#include <memory>
class SDL2_Music {
public:
bool loadMusic(const std::string& path);
bool play(int loops = -1);
// more methods
private:
std::unique_ptr<Mix_Music> music_ = nullptr;
};
/* cpp file */
bool SDL2_Music::loadMusic(const std::string& path) {
music_ = std::make_unique<Mix_Music>(Mix_LoadMUS(path.c_str()));
if (music_ == nullptr) {
ktp::logSDLError("Mix_LoadMUS");
return false;
}
return true;
}
// more stuff
When I try to compile (GCC) I get:
"C:Users
ot_bjarneCodeBlocksMinGWlibgccx86_64-w64-mingw328.1.0includec++itsunique_ptr.h|831|error:
invalid use of incomplete type 'struct _Mix_Music'"
And codeblocks points me to unique_ptr.h
, which I obviously didn't tried to "fix".
question from:
https://stackoverflow.com/questions/65942724/why-im-getting-invalid-use-of-incomplete-type-error-when-i-change-raw-pointer 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…