CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2010
    Posts
    4

    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;
    }
    }

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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.

  3. #3
    Join Date
    Nov 2007
    Posts
    35

    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&#37;2B%2B0x#Extern_template

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Oct 2010
    Posts
    4

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

  6. #6
    Join Date
    Oct 2010
    Posts
    4

    Re: Function Template

    All right renaming helper.cpp to helper.h does work...i guess that's what meant by instantiation?

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Function Template

    Quote Originally Posted by mediocre View Post
    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.

  8. #8
    Join Date
    Nov 2007
    Posts
    35

    Re: Function Template

    Quote Originally Posted by mediocre View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured