samedi 15 février 2020

Recursive including template files fails compilation with "no named template"

I am trying to use a simple template class to decode different types. I am getting this error

 NestedType.h:12: error: no template named 'Decoder'
    Decoder<int> m_value;
    ^

Actually its a part of big project but I am sharing a simple project to show this specific problem. Please help to resolve this issue, also provide some reference to read more about this template issue.

These are the four files in the project

main.cpp

#include <iostream>
#include <Decoder.h>
#include <NormalType.h>
#include <NestedType.h>

using namespace std;
using namespace CheckDecoder;
int main()
{
    Decoder<NormalType> d_normal;
    d_normal.decode(std::cin);

    Decoder<NestedType> d_nested;
    d_nested.decode(std::cin);
    return 0;
}

Decoder.h

#ifndef DECODER_H
#define DECODER_H

#include <iostream>

#include "NormalType.h"
#include "NestedType.h"
namespace CheckDecoder {


template<typename T>
class Decoder
{
public:
    void decode(std::istream& istr)
    {
        // some complex code
        CheckDecoder::decode(m_value, istr);
    }

    T m_value;
};

}
#endif // DECODER_H

NormalType.h

#ifndef NORMALTYPE_H
#define NORMALTYPE_H

#include <iostream>

namespace CheckDecoder {

class NormalType
{

};

void decode(NormalType& t, std::istream& istr)
{
    // do something
}
}
#endif // NORMALTYPE_H

NestedType.h

#ifndef NESTEDTYPE_H
#define NESTEDTYPE_H

#include <Decoder.h>

namespace CheckDecoder {

class NestedType
{

private:
    Decoder<int> m_value;
};

void decode(NestedType& t, std::istream& istr)
{
    // do something
}
}

#endif // NESTEDTYPE_H

Aucun commentaire:

Enregistrer un commentaire