0

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.

masterop
  • 184
  • 1
  • 12
  • 1
    which lines are 54,60 and 65? What did you modify? Was it working before? – 463035818_is_not_an_ai Jan 24 '17 at 09:18
  • The code was working before and I just removed the inclusion of some header files which were not needed for my project. The lines have been mentioned now in the code. – masterop Jan 24 '17 at 09:27
  • before it was working, you removed some includes, now it is not working. Why do you still think those includes are not needed? – 463035818_is_not_an_ai Jan 24 '17 at 09:34
  • Because that part of code is not needed and that does not explain why the compiler is giving error at these lines of code. – masterop Jan 24 '17 at 09:38
  • I appreciate your tenacity, but dont you see that the code you show here is incomplete? There are many declarations missing: `ParameterCode`, `ParameterAttributes`, etc. are not declared anywhere (but maybe in the headers you removed) – 463035818_is_not_an_ai Jan 24 '17 at 09:41
  • all the types that you see in the code snippet are declared in this file before this code. As I wrote in my question that only the relevant part of the code is copied here. This file has around 1000 lines of code. I do not need a work-around, I am trying to understand the problem. – masterop Jan 24 '17 at 09:47
  • Sometimes ncludes which seems unnecessary hides required includes inside. In this case you just should include them directly. – Zefick Jan 24 '17 at 09:50
  • 1
    By adding missing part, I cannot reproduce problem [Demo](http://coliru.stacked-crooked.com/a/9a9cbc80eccdc9f2). Provide a MCVE. – Jarod42 Jan 24 '17 at 09:55

1 Answers1

2

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Zefick
  • 2,014
  • 15
  • 19
  • The problem was that somewhere else, I was instantiating an object of this template with a class that was not available because of the deletion of header files. So, I deleted that part of the code as well. The problem was that compiler was not complaining there :) – masterop Jan 27 '17 at 08:13