..Extern Template in .h file

This question has been asked around the web but I'm struggling to understand the
replies..

1
2
3
4
5
6
7
In my .cpp file I have the below.

    Template<class myvar>
    myvar myfunction(myvar, int x)
    {
      return x+5;
    }


In my header file .h I have the below. But can't seem to compile this. I'm not sure I understand why? How do I do this properly?

1
2
extern Template<class myvar>
    myvar myfunction(myvar, int x);
Last edited on
Templates need to be in a single .h file
That's a good answer Yanson.. On most forums this would be called trolling. On this forum it seems that this is perfectly acceptable behavior. Trickle feed your response .. That's it!
Templates need to be in the header file. This is so what is being compiled has access to the template code so it can be instantiated for a type when that type is used.
1
2
3
4
5
6
In my header file .h I have the below. But can't seem to compile this. I'm not sure I understand why? How do I do this properly?

1
2
extern Template<class myvar>
    myvar myfunction(myvar, int x);


Does it not look like the above is in my header file? Can you tell me where I have gone wrong?
The template declaration and definition both need to be in the .h header file. You're trying to put the declaration in the .h file and the definition in the .cpp file. That won't work.
ok, well again...... I'm not sure that the below will work
The below is in the header file.


1
2
3
Template<class myvar>
extern Template<class myvar>
    myvar myfunction(myvar, int x);
Last edited on
I don't mean to sound rude, but I'm not sure what part of our responses you're not understanding. Let me start from the beginning:

Generally, programs are split into two files. (1) The .h file which usually implements function declarations or prototypes, which are later defined in the (2) .cpp file. See my example below:

1
2
3
4
5
6
//myProgram.h
class myClass
{
public:
   void myFunction(); // the declaration/prototype
};


1
2
3
4
5
6
7
//myProgram.cpp
#include "myProgram.h"

void myClass::myFunction()
{
   // here is the implementation
}


You're trying to do that same thing with a template function. You're trying to supply the definition of the template function in the .cpp file and the declaration in the .h file. This will not work. What you need to do is supply both the declaration, and the definition in the .h file.

1
2
3
4
5
6
//myProgram.h
Template<class myvar>
    myvar myfunction(myvar, int x)
    {
      return x+5;
    }


That's it. You're done. Get rid of any mention of the template function in .cpp file.
Thanks Aaron.. You're not sounding rude.
It's those trolls which come in and provide the very minimum answer with no explanation as to what they are talking about which sounds rude.
I appreciate your effort.
I have also reported Yanson for doing just that. I work for an organization and am trying to solve a few issues. Yanson's kind of trolling behaviour just wastes time.. Nothing more.
That's not what the report button is for. Not every answer that doesn't satisfy your standards is trollish or abusive. If you have a problem with an answer someone gives you, you can ask for a clarification.
Although rather terse, Yanson's answer is perfectly correct, so I have no idea what you consider to be "trolling".
Topic archived. No new replies allowed.