简体   繁体   中英

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

I am modifying some existing code in a project. I have a template class Param and a partial specialized template class for parameters in a header file as below:

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

in the specialization of template class I get this error message: "incomplete type is not allowed Parameters.h" at line:65 "pointer to incomplete class type is not allowed Parameters.h" at line:54 and 60

I know that the specialization of template class has at incomplete type but the compiler should resolve it at compile time, or do I need some sort of forward-declaration. Both the classes are in same .h file. Only the relevant code is copied here. Thanks for your help.

You probably forgot to include a header file with definition of a parameter class somewhere. This is the most common cause of the error.

Forward declaration works when you do not use fields from the declared class. Once you start using fields, you need a definition of this class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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