模板化的配置值类和全局列表
templated config value class and global list
嘿,我正试图实现一个简单的配置值类,其中包含这些值的全局列表。我的实现是这样的:
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;
现在编译得很好,但是给了我两个链接错误:
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)
我相信这与我在顶部创建的那种循环依赖关系有关。我该如何解决这个问题,或者是否有更好的方法来使用模板?我不想为我想支持的每种类型创建一个类。
链接器错误总是相同的错误:您的链接器没有代码。一旦你明白了这一点,你就可以很容易地解决链接错误:)在本例中,缺少的代码是静态变量的定义。在你的类中,你声明它们,但是你没有定义它们。下面是定义类模板的静态变量的语法:使用静态变量和模板
我认为你在这里把模板类弄乱了(它们是c++中很难的一部分,所以不要对此感到不满:)
我想这就是你想要做的:
#include "stdafx.h"
#include <string>
using namespace std;
template <typename T>
class ConfigSetting {
public:
static ConfigSetting* head; // I fixed here, no need for Global, you have this class static vars
static ConfigSetting* tail;
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 (head) {
ConfigSetting* temp = head;
while (!temp) {
if (temp->m_name == name) {
return temp; // I fixed here, you have to return the object you find
}
temp = temp->m_next;
}
}
return nullptr;
}
private:
void addToList() // no need for 'void' this is C++, not C
{
if (head) {
tail->m_next = this;
tail = this;
}
else {
head = this;
tail = this;
}
}
ConfigSetting* m_next;
const std::string m_name;
const std::string m_synopsis;
T m_value;
T m_min;
T m_max;
};
template <typename T>
ConfigSetting<T>* ConfigSetting<T>::head = nullptr;
template <typename T>
ConfigSetting<T>* ConfigSetting<T>::tail = nullptr;
int _tmain(int argc, _TCHAR* argv[])
{
ConfigSetting<int> j("name", "syn", 1);
return 0;
}
在类外定义静态的head
和tail
指针
template<typename T>
ConfigSetting<T>* Global<T>::tail = nullptr ;
template<typename T>
ConfigSetting<T>* Global<T>::head = nullptr ;
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 当vector是tje全局变量时,c++中vector的内存管理
- 复制列表初始化的隐式转换的等级是多少
- C++娜娜 如何从全局列表框*指针访问列表框内的索引 0 上的按钮?
- std::初始值设定项列表全局/静态对象的生存期
- 如何将多个位图包含在全局数组列表中
- 具有空捕获列表的Lambda仍然能够从全局范围捕获对象
- 从C++源文件创建全局变量列表
- 使用PThreads访问全局列表-指针问题
- 如何使用Clang获取C/ c++程序的所有全局声明列表
- CMAKE:在函数中创建和构建一个列表——使用目录或全局作用域
- 浏览列表,每次传递3个对象时重复调用全局函数
- 在一个列表中导航,该列表将对象从列表传递到全局方法c++
- 模板化的配置值类和全局列表