分部模板类中不允许指向不完整类类型的指针

pointer to incomplete class type is not allowed in a partial template class

本文关键字:类型 指针 不允许      更新时间:2023-10-16

我正在修改项目中的一些现有代码。我有一个模板类 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 文件中。此处仅复制相关代码。感谢您的帮助。

您可能忘记在某处包含一个包含参数类定义的头文件。这是错误的最常见原因。

当您不使用声明类中的字段时,前向声明有效。开始使用字段后,您需要此类的定义。