Pergunta

When I create a function that takes in a typename, I can create it fine without a class, but when I try to put the functionality inside a class it gives me errors. Could anyone explain to me what I have to do to get it working and why?

Example of working case: This is when I don't put it inside a class

template<typename T>
bool Test(const char* _pcSection, const char* _pcKey, T& _tValue)
{
    return true;
}

Example of failing: When I try to chuck it inside the class (so I can access member variables)

class CIniParser
{
    public:
        template<typename T>
        bool GetValue(const char* _pcSection, const char* _pcKey, T& _tValue);
}

/////////////////////////
//Inside the .cpp...
template<typename T>
bool CIniParser::GetValue(const char* _pcSection, const char* _pcKey, T& _tValue)
{
    //do stuff
    return true;
}

Any help would be great :)

Foi útil?

Solução

Nothing actually gets compiled until you instantiate an actual instance of the template class. Therefore, it makes no sense to put the function definitions in a cpp: they need to be visible to every compilation unit making use of the template.

The normal thing to do is to put the function definitions in the same header as the template declaration.

(You can put the whole of the template declarations and definitions in a source file but only if their sole use is in that file).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top