object.h
#ifndef TESTFACTORYMETHOD_OBJECT_H
#define TESTFACTORYMETHOD_OBJECT_H
#include <string>
#include <unordered_map>
#include <functional>
class Object;
using RegistryFormatMap = std::unordered_map<std::string, std::string>;
using RegistryConstructorMap = std::unordered_map<std::string, std::function<Object*(const std::string &)>>;
class Object {
public:
static RegistryFormatMap registryFormatMap;
static RegistryConstructorMap registryConstructorMap;
private:
};
#endif //TESTFACTORYMETHOD_OBJECT_H
apple.h
#ifndef TESTFACTORYMETHOD_APPLE_H
#define TESTFACTORYMETHOD_APPLE_H
#include "object.h"
class Apple : public Object{
public:
constexpr static char m_name[6] = "Apple";
std::string m_test_val;
class Factory {
public:
Factory(){
std::string temp = m_name;
Object::registryFormatMap[temp] = "test_apple";
Object::registryConstructorMap[temp] = ([](const std::string &in) -> Apple * { return new Apple(in); });
}
};
static Factory factory;
explicit Apple(const std::string& str) {
m_test_val = str;
}
private:
};
#endif //TESTFACTORYMETHOD_APPLE_H
main.cpp
#include <iostream>
#include "object.h"
int main() {
for(const std::pair<std::string, std::string>& pair : Object::registryFormatMap) {
std::cout << pair.second << std::endl;
}
return 0;
}
I get the following error
:main.cpp:(.rdata$.refptr._ZN6Object17registryFormatMapB5cxx11E[.refptr._ZN6Object17registryFormatMapB5cxx11E]+0x0): undefined reference to `Object::registryFormatMap[abi:cxx11]'
I'm using mingw64 on windows to compile. I'm attempting to follow the factory method pattern found here but in a way such that I can expand it latter into a macro that doesn't require extensions of object to have to manually enter any of the boiler plate code. I think its possible that Object::registryFormatMap
is not initialized but then I would expect run-time errors, not compile time errors.
I'm trying to make two maps, a string format map and a lambda pointer to a constructor map to allow classes to both be initialized and give what they need to be initialized via string format to some external function. In a separate code base I have a lot of these classes, and I'm trying to make it such that I don't have to edit multiple files every-time I add a new class that fits the "Object" format, previously I was using manually created unordered_map
s.