错误 C2908:显式专用化; 已被实例化
error C2908: explicit specialization; '' has already been instantiated
>我有一个简单的模板单例类,我简化了如果保持简单易读(删除了保护措施,断言等,这不是问题的主题(
template< class T> T* Create();
template <class T>
class CSingleton
{
public:
static T* CreateInstance() { return m_instance = Create<T>(); }
static void DestroyInstance() { delete m_instance;}
protected:
static T* m_instance;
};
如您所见,我使用全局函数 T* Create(( 来新建指针,因为我的类可能是一个抽象类。 所以如果我像这样定义 CreateInstance:
static T* CreateInstance() { return m_instance = new T; }
它将生成一个错误,我无法实例化抽象类。
所以这里有一个非常简单的例子,产生错误,班级 :
class MyClass : public CSingleton<MyClass>
{
};
以及我的 CPP 中全局函数的定义
template< > MyClass* Create< MyClass >()
{
return nullptr;// just for the compilation demonstration
}
如果我不定义这个,链接器将输出它找不到 Create(( 的错误,如果我定义函数,我得到这个错误:
error C2908: explicit specialization; 'T *Create<T>(void)' has already been instantiated
error C2908: with
error C2908: [
error C2908: T=MyClass
error C2908: ]
我在这个问题上被困了 2 个小时,我找不到解决方案,我在 StackOverflow 上搜索了谷歌和搜索,找不到类似的问题和解决方案。
干杯塞布
所有这些类都在 DLL 库中,但我仅在构建库时遇到编译器/链接错误。
我忘了检查dllimport/dllexport签名。在我的单例类前面添加它们并创建函数解决了此错误。
相关文章:
- 静态数据成员模板专用化的实例化点在哪里
- 您能否实例化模板的非专用版本并在专用化中继承它?
- 如果专用化已经隐式实例化,它是否隐式实例化?
- 在 Xcode 中实例化后的显式专用化
- 是具有接口专用化的子类多态的模板实例化
- 在实例化封闭类模板之后,我们可以声明模板类成员的部分专用化吗
- 在模板中,有没有办法为每个时间实例化只编写一个专用化?(纳秒、毫秒、秒等)
- 错误 C2908:显式专用化; 已被实例化
- 一种安全、符合标准的方法,使类模板专用化仅在实例化时才无法使用"static_assert"进行编译
- 对实例化和未实例化模板的部分模板专用化
- 模板类方法的部分专用化或实例化
- 如何将包装作为模板参数实例化专用模板类
- 共享库:具有部分模板专用化和显式模板实例化的未定义引用
- 模板专用化的实例化不正确
- MSVC 编译器实例化函数模板的默认定义,即使存在专用化
- 声明无法解决"实例化后的显式专用化"错误
- 为非专用模板实例化强制执行编译错误
- 如何正确地显式实例化具有完全专用成员的模板类
- 在完全专用的模板函数中实例化类的对象
- 无法强制实例化专用模板