g**u 发帖数: 15 | 1 Hi,
i am learning C++ template now. I decleared serveral template functions in a
head file and implemented them in a cpp file. In main function located in
mainfile I call these template functions, but everytime I tried, there was a
link error Lni 2001. When I move the declear and implementment to mainfile and
then call them, it's fine. In another way, I changed all the template
functions to normal functions in the head file and cpp file, the program works
well. So what's the problem?
Thanks | c********e 发帖数: 383 | 2 your problem is that the compiler doest instantiate he template anywhere.
you could try.
way 1. in your .h file, at the end include the cpp file.
way 2. use explicit template instantiation.
both have pros and cons, since they are very closely relative to compiler im
plementation lets leave it alone here.
【在 g**u 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Hi, : i am learning C++ template now. I decleared serveral template functions in a : head file and implemented them in a cpp file. In main function located in : mainfile I call these template functions, but everytime I tried, there was a : link error Lni 2001. When I move the declear and implementment to mainfile and : then call them, it's fine. In another way, I changed all the template : functions to normal functions in the head file and cpp file, the program works : well. So what's the problem? : Thanks
| P*****f 发帖数: 2272 | 3 or find a compiler supporting "export"
【在 c********e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : your problem is that the compiler doest instantiate he template anywhere. : you could try. : way 1. in your .h file, at the end include the cpp file. : way 2. use explicit template instantiation. : both have pros and cons, since they are very closely relative to compiler im : plementation lets leave it alone here.
| s*******y 发帖数: 558 | 4 For .Net, you can't include .cpp in .h.
Just put template definition and implementation together
in the same file and name it as .hpp (means header and cpp),
and then include this .hpp in ur other .cpp file.
Under Linux or Cygwin, u can separate the declaration and
implementation of template. But u have to include .cpp of
the template in .h. For example, to declare a template class
myTemplate.H
#ifndef MY_TEMPLATE_
#define MY_TEMPLATE_
temaplate
class example{
...
};
#include "myTempla
【在 g**u 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Hi, : i am learning C++ template now. I decleared serveral template functions in a : head file and implemented them in a cpp file. In main function located in : mainfile I call these template functions, but everytime I tried, there was a : link error Lni 2001. When I move the declear and implementment to mainfile and : then call them, it's fine. In another way, I changed all the template : functions to normal functions in the head file and cpp file, the program works : well. So what's the problem? : Thanks
|
|