Hey there i am trying to implement a simple config value class with a global list which holds these values. My implementation looks like this:
template <typename Y>
class ConfigSetting;
template <typename T>
class Global {
public:
static ConfigSetting<T>* head;
static ConfigSetting<T>* tail;
};
template <typename T>
class ConfigSetting {
public:
ConfigSetting(const std::string& name, const std::string& synopsis, T initValue) : m_name(name), m_synopsis(synopsis), m_value(initValue)
{
this->addToList();
}
static ConfigSetting* findSetting(const std::string& name)
{
if (Global<T>::head) {
Global<T> temp = Global<T>::head;
while (!temp) {
if (temp->m_name == name) {
return Global<T>::head;
}
temp = temp->m_next;
}
}
return nullptr;
}
private:
void addToList(void)
{
if (Global<T>::head) {
Global<T>::tail->m_next = this;
Global<T>::tail = this;
}
else {
Global<T>::head = this;
Global<T>::tail = this;
}
}
ConfigSetting* m_next;
const std::string m_name;
const std::string m_synopsis;
T m_value;
T m_min;
T m_max;
Now this compiles fine, but gives me 2 linker errors:
error LNK2001: unresolved external symbol "public: static class ConfigSetting<int> * Global<int>::head" (?head@?$Global@H@@2PAV?$ConfigSetting@H@@A)
error LNK2001: unresolved external symbol "public: static class ConfigSetting<int> * Global<int>::tail" (?tail@?$Global@H@@2PAV?$ConfigSetting@H@@A)
I believe that this has something to do with the kind of circular dependency i created at the top. How can i solve this, or is there a better way doing this with templates? I did not want to create one class for each type i want to support.