|
-
October 20th, 2010, 12:23 PM
#1
Function Template
I am new to function template. I found that the following code gives an error. The error returned is:
error LNK2019: unresolved external symbol "void __cdecl linspace<double>(double *,double,double,int)" (??$linspace@N@@YAXPANNNH@Z) referenced in function _wmain
The following are my codes:
In main.cpp:
#include "stdafx.h"
#include "helper.h"
int _tmain(int argc, _TCHAR* argv[])
{
int m=91,n=61;
int i,j;
double* x,y;
double X[]={0.0,1.5},Y[]={0.0,1.0};
double*psi;
x=new double[n];
linspace<double>(x,X[0],X[1],n);
....
In helper.h
template <typename T>
void linspace(T*x,T start,T end,int length);
In helper.cpp
template <typename T>
void linspace(T* x,T start,T end,int length)
{
T h;
h=(end-start)/(length-1);
for(int i=0;i<length;i++)
{
x[i]=start+i*h;
}
}
-
October 20th, 2010, 12:32 PM
#2
Re: Function Template
I don't have much experience with templates, but AFAIK their implementation needs to be available at compile time. So try to discard your helper.h and rename helper.cpp to helper.h instead.
HTH
Please use code tags when posting code.
Ah, and... Welcome to CodeGuru!
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
October 20th, 2010, 01:56 PM
#3
Re: Function Template
I haven't tried it to see if it's implemented in vc++ 2010 but for grins you
can try the declaration as shown here:
http://en.wikipedia.org/wiki/C%2B%2B0x#Extern_template
-
October 20th, 2010, 02:38 PM
#4
Re: Function Template
As far as I know VS2010 does not support that.
Anyway, as I understand it an instantiated template do have to exist even if it now can be extern. Do you link with something that do instantiate that template?
-
October 20th, 2010, 09:14 PM
#5
Re: Function Template
I am not too familiar with the concept of instantiation. Doesn't this part of the code instantiate the function template?
In helper.h
template <typename T>
void linspace(T*x,T start,T end,int length);
-
October 20th, 2010, 09:18 PM
#6
Re: Function Template
All right renaming helper.cpp to helper.h does work...i guess that's what meant by instantiation?
-
October 21st, 2010, 03:52 AM
#7
Re: Function Template
 Originally Posted by mediocre
i guess that's what meant by instantiation?
Well, instantiation means turning an abstract concept of a something into a concrete something (the instance).
Maybe this isn't really precise, but I like to explain the difference between a class and a template like this, for the simplicity of the explanation: While instantiating a class gives you an object, instantiating a template gives you either a class or a function (like in the case of your code). Consequently, the abstraction level of a template is one level higher than that of a class.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
October 21st, 2010, 06:08 PM
#8
Re: Function Template
 Originally Posted by mediocre
All right renaming helper.cpp to helper.h does work...i guess that's what meant by instantiation?
You have to plug in an actual type at some point instead of just Type T for the compiler to generate code. Otherwise it's like using variables that are never assigned a value. 2 * x is what if nobody ever said what 'x' is?
The cookie cutter needs some real dough.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|