由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Help C++ Template function link error .
相关主题
C++ template problemwhere to define my template function
基础问题:在header里面define functionboost::function 的 syntax 问题
一个 C++ STL base type 的问题template 类的继承问题
请教template和factory有啥区别?C++ template questions
弱问c++里有没有NULL这个keyword?Question in C++ complex number implementation
又一个初级问题: C++中多如牛毛的#define格式C++ template
弱问C++一个问题 一直不解C++ class template specialization question
inline functions in C++One c++ non-type template question
相关话题的讨论汇总
话题: template话题: cpp话题: file话题: c++话题: functions
进入Programming版参与讨论
1 (共1页)
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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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

1 (共1页)
进入Programming版参与讨论
相关主题
One c++ non-type template question弱问c++里有没有NULL这个keyword?
第五版的 c++ primer 出来了又一个初级问题: C++中多如牛毛的#define格式
qeustion about separation of interface and implementation in java弱问C++一个问题 一直不解
请教C++中的unordered_setinline functions in C++
C++ template problemwhere to define my template function
基础问题:在header里面define functionboost::function 的 syntax 问题
一个 C++ STL base type 的问题template 类的继承问题
请教template和factory有啥区别?C++ template questions
相关话题的讨论汇总
话题: template话题: cpp话题: file话题: c++话题: functions