分部模板类中不允许指向不完整类类型的指针
pointer to incomplete class type is not allowed in a partial template class
我正在修改项目中的一些现有代码。我有一个模板类 Param 和一个用于头文件中参数的部分专用模板类,如下所示:
template<class ValueType>
struct Param
{
static void add(ParameterCode code, ValueType value, ParameterAttributes attributes)
{
}
static PreSetResultType preSetActions(ParameterCode code, ValueType& value)
{
return (SUCCESS);
}
static void set(ParameterCode code, ValueType value)
{
}
static ValueType get(ParameterCode code)
{
ValueType value = ValueType();
return (value);
}
static void serialize(ParameterCode code, vector<uint8_t>& byteArray)
{
}
static ValueType deserialize(const vector<uint8_t>& byteArray)
{
return (*reinterpret_cast<const ValueType*>(byteArray.begin()));
}
};
/* template function specialization for pointer types */
template<class ValueType>
struct Param<ValueType*>
{
static void add(ParameterCode code, ValueType* value, ParameterAttributes attributes)
{
}
static PreSetResultType preSetActions(ParameterCode code, ValueType*& config)
{
return (SUCCESS);
}
static void set(ParameterCode code, ValueType* pObjNew)
{
pObjNew->serialize(serializedObject); //error line 54
}
static ValueType* get(ParameterCode code)
{
void* value = NULL;
return ((ValueType*)value);
}
static void serialize(ParameterCode code, vector<uint8_t>& byteArray)
{
ValueType* obj = get(code);
obj->serialize(byteArray); //error line 60
}
static ValueType* deserialize(const vector<uint8_t>& byteArray)
{
return (ValueType::deserialize(byteArray)); //error line 65
}
};
在模板类的专业化中,我收到以下错误消息:"不允许不完整的类型 参数.h" 在第 65 行"不允许指向不完整类类型的指针 Parameters.h"在第 54 和 60 行
我知道模板类的专用化类型不完整,但编译器应该在编译时解决它,或者我需要某种前向声明。这两个类位于同一个 .h 文件中。此处仅复制相关代码。感谢您的帮助。
您可能忘记在某处包含一个包含参数类定义的头文件。这是错误的最常见原因。
当您不使用声明类中的字段时,前向声明有效。开始使用字段后,您需要此类的定义。
相关文章:
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 如何访问在 c++ 中在类内声明的结构类型指针变量?
- (C )找到基本类型指针的儿童类型
- 表达式必须具有指向对象的指针类型(指针向量)
- 将子项复制构造到父类型指针中
- C 返回类型指针声明
- 调用虚拟函数而不通过类类型指针创建任何对象
- C++ 如何使用类类型指针制作向量
- 为什么基类型指针不能获取派生类对象的地址值?
- 如何在 C++ 中获取映射类型指针,映射
- 如何检查该类型的类型指针是正确对齐的
- 正在转换为短类型指针
- 指向任意类方法的模板非类型指针
- 如何通过强制转换类型指针将字符数组转换为uint16_t
- 指向shared_ptr的不透明类型 C 指针
- C++:从值类型指针强制转换为包含迭代器
- C/C++:访问给定类型指针的位置与访问另一类型指针的相同位置不同
- 比较类型指针
- 数据类型指针使用*(Datatype *)
- 如何打印c++中char类型指针的所有值