繁体   English   中英

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

[英]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
    }
};

在模板类的专业化中,我在第65行收到此错误消息:“不允许不完整类型的Parameters.h”在第54行和第60行“指向不完全类类型的指针Parameters.h”

我知道模板类的专业化具有不完整的类型,但是编译器应在编译时解决它,还是我需要某种前向声明。 这两个类都在同一个.h文件中。 仅将相关代码复制到此处。 谢谢你的帮助。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM