C 不能将带有父类指针作为类型的静态模板成员定义引用
c++ cant define a reference to static template member with the parent class pointer as a type
我要做的事情:
为我的班级制作某种"注册表",以便可以使用唯一字符串查找每个实例(我正在检查其唯一的,但在下面的示例中省略了(。
我的问题:
我不能声明对我的静态成员的引用。我不知道为什么。
代码(省略零件(
//component.h
template <class t>
class ComponentTable : InstanceCounted<ComponentTable<t>> //Instance Counted is a template that automatically counts the instances
{
private:
//Omitted
static std::unordered_map<std::string, ComponentTable*> componentRegistry;
public:
//Omitted, the insert happens in the constructor and it gets removed in the destructor
}
//component.cpp
template<class t>
std::unordered_map<std::string, ComponentTable<t>*> ComponentTable<t>::componentRegistry;
//---
我已经尝试了
- 将
ComponentTable*
更改为标题中的ComponentTable<t>*
- 检查标题和CPP文件中的拼写。
- 重建项目
我真的没有铅,所以我有点尝试:(
错误:
undefined reference to `ComponentTable<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::componentRegistry'|
这将出现在我试图首先使用构造函数中的成员的位置。我正在使用简单的std ::对正确类型的插入函数进行插入。
其他数据:
- 使用CodeBlocks制造并在C 14模式下使用Mingw编译。
我希望此信息足以帮助我在这里;(
由于您正在处理模板,因此应定义标题本身中的静态成员:
//component.h
template <class t>
class ComponentTable : InstanceCounted<ComponentTable<t>>
{
static std::unordered_map<std::string, ComponentTable*> componentRegistry;
};
template<class t>
std::unordered_map<std::string, ComponentTable<t>*> ComponentTable<t>::componentRegistry;
这是确保为每个ComponentTable<t>
安装componentRegistry
的最简单方法。
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 如何在C++中使用非静态成员函数作为回调函数
- (C++)为什么静态成员可以在初始化之前使用
- 类的全局对象和静态成员
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 模板化类中静态成员的延迟初始化
- 使用静态成员声明类时遇到问题
- C++:是否可以使用非静态成员变量模板?
- 静态成员函数使用相同的名称时出现模板类型名称错误
- 如何在复杂继承中访问静态成员变量
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 如何在友元函数中使用静态成员而不添加前缀 [类名]::
- C++构造函数和静态成员
- 为什么传递非静态成员函数会导致编译错误?
- 非静态成员失败的线程调用函数
- 静态成员变量不会由 gettext 转换
- decltype:使用指针访问类的静态成员
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 非静态成员函数的 decltype 格式不正确吗?
- 如何在C++中定义静态成员结构