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

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

我正在修改项目中的一些现有代码。我有一个模板 class 参数和一个部分专用模板 class 用于头文件中的参数,如下所示:

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
    }
};

在模板的专业化 class 中,我收到以下错误消息: "incomplete type is not allowed Parameters.h" 在 line:65 "pointer to incomplete class type is not allowed Parameters.h" 在 line:54 和 60

我知道模板 class 的特化有不完整的类型,但编译器应该在编译时解决它,或者我需要某种前向声明。 classes 都在同一个 .h 文件中。 这里只复制相关代码。 感谢您的帮助。

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

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