0

Inspired by GoTW 101, I decided to try and apply the Pimpl idiom to a part of a library I'm working on using the pimpl wrapper described there.

However, when trying to compile, I get an undefined reference to the destructor of that Pimpl class. This only happens with clang and only on Linux. I.e., GCC on Linux and Clang on OSX work fine.

Here's the code that's not working:

Header file

#ifndef TANK_MUSIC_HPP
#define TANK_MUSIC_HPP

#include <string>
#include "../Utility/Pimpl.hpp"

namespace tank
{

class Music
{
    struct impl;
    Pimpl<impl> data;

public:
    Music(std::string fileName);
    //~Music();

    bool load(std::string fileName);

    void play();
    void pause();
    void stop();
};

}

#endif //TANK_MUSIC_HPP

Source file

#include "Music.hpp"
#include "../System/Game.hpp"
#include <SFML/Audio/Music.hpp>
#include "../Utility/Pimpl_impl.hpp"

namespace tank
{

struct Music::impl {
    sf::Music music;
    bool loaded = false;
};

Music::Music (std::string fileName)
{
    load(fileName);
}

//Removing this comment works, but this shouldn't be necessary should it?
//Music::~Music() = default;

bool Music::load (std::string fileName)
{
    if (data->loaded)
    {
        Game::log << "Music already loaded!" << std::endl;
        return data->loaded;
    }
    data->loaded = data->music.openFromFile(fileName);
    return data->loaded;
}

void Music::play()
{
    data->music.play();
}

void Music::pause()
{
    data->music.pause();
}

void Music::stop()
{
    data->music.stop();
}

}

Any ideas would be appreciated, this has had me scratching my head for a while now!

Community
  • 1
  • 1
dtruby
  • 1,775
  • 2
  • 12
  • 9
  • why you remove the explicit destructor, you need it, it is explained on the same page. – yngccc Jan 17 '14 at 22:31
  • [To quote MSN:](http://stackoverflow.com/a/8595501/103167) **"you need to add an explicit instantiation of the `pimpl<...>` template"** – Ben Voigt Jan 17 '14 at 22:32
  • I thought it was odd that you didn't need the destructor etc., but what confused me was that GCC and Clang on OSX compile this fine. Any ideas why they might work where clang on Linux fails? – dtruby Jan 17 '14 at 22:39

0 Answers0