0

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 :)

Matt
  • 175
  • 2
  • 12
  • 1
    define the method inlined in the *.h – jsantander Apr 04 '14 at 08:40
  • 3
    [Why can templates only be **implemented** in header files?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – WhozCraig Apr 04 '14 at 08:41
  • 2
    "It gives me errors" is not an appropriate way to communicate on Stack Overflow. – Kerrek SB Apr 04 '14 at 08:43
  • @KerrekSB: Seconded. The actual error text is extremely valuable. – Bathsheba Apr 04 '14 at 08:44
  • @KerrekSB Thirded. I am actually amazed that people are able to discard error messages as useless noise, while they exist for the very specific goal to help people diagnose their errors. – SirDarius Apr 04 '14 at 08:48
  • 1
    @Bathsheba Seconded again (I almost said thirded). It's not an appropriate way to communicate, period. Error handling was written by people who wanted to help you write code :) – keyser Apr 04 '14 at 08:48
  • @ᴋᴇʏsᴇʀ i beat you to it, eh ! – SirDarius Apr 04 '14 at 08:49
  • @SirDarius Yup, between refreshes :) – keyser Apr 04 '14 at 08:50
  • I decided to omit the error code as I felt it unnecessary. I understand the error code means that the function definition cannot be found, hence the "reactive response" of the compiler was accounted for. The actual reason I was getting the error was not. I think I provided enough information to get the answer I expected, and the first two comments and first answer show I was right to think so :) Thanks for the help though, I agree error codes always help – Matt Apr 04 '14 at 10:15

1 Answers1

1

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).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Normal here depends on the shop. The "normal" thing to do is to put the function definitions for templates in a separate, specially named file, and include that at the end of your header. (The most frequent convention I've seen is `.tcc`, but that's always been in a Unix world where the normal sources were `.cc` and the normal headers were `.hh`. I don't know what the convention would be in places where the normal sources are `.cpp`.) – James Kanze Apr 04 '14 at 08:55
  • Thank you for the short and concise answer, I just chucked the function into the .h and it's working fine. It's been an issue for a long time, and while the ones I kept were the ones I always defined in the .h, I was under the impression it was putting it inside a class that caused the issue, not where it was. Now I understand why. Thanks :) – Matt Apr 04 '14 at 09:41