I have a class ResourceManager
that I try to use as a singleton here. It has a file resources.hpp
and resources.cpp
that I list below. The last code listing is the usage of ResourceManager::instance()
and the error message below. I tried a lot of things like forward declarations etc. but could not find a way to let the compiler succeed.
resources.hpp
namespace X11 {
class ResourceManager {
private:
static ResourceManager* _instance;
ResourceManager() {
//logic
};
ResourceManager (const ResourceManager&);
~ResourceManager() = default;
public:
static ResourceManager* instance();
std::vector<sf::Font> fonts;
};
}
resources.cpp
#include "resources.hpp"
namespace X11 {
ResourceManager* ResourceManager::_instance = 0;
ResourceManager* ResourceManager::instance() {
if (!ResourceManager::_instance)
ResourceManager::_instance = new ResourceManager();
return ResourceManager::_instance;
}
}
Usage (initialized.cpp)
#include "initializer.hpp"
// initializer.hpp includes "resources.hpp"
void Initializer::init_right_bar(Menu& menu) {
// logic
text.setFont(ResourceManager::instance()->fonts[0]);
// logic
}
Error message
[ 30%] Building CXX object CMakeFiles/vilsoc.dir/src/initializer.cpp.o
[ 40%] Building CXX object CMakeFiles/vilsoc.dir/src/vilsoc.cpp.o
[ 50%] Linking CXX executable vilsoc
/usr/bin/ld: CMakeFiles/vilsoc.dir/src/initializer.cpp.o: in function `X11::Initializer::init_right_bar(X11::Menu&)':
initializer.cpp:(.text+0x1715): undefined reference to `X11::ResourceManager::instance()'
collect2: error: ld returned 1 exit status
SOLVED The comments led to the conclusion that I wasn't updating my makefile through CMake and therefore the linker could not find the source file.