Redian新闻
>
Help C++ Template function link error .
avatar
Help C++ Template function link error .# Programming - 葵花宝典
g*u
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
avatar
c*e
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 的大作中提到】
: 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

avatar
P*f
3
or find a compiler supporting "export"

【在 c********e 的大作中提到】
: 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.

avatar
s*y
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 的大作中提到】
: 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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。