由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ template problem
相关主题
Help C++ Template function link error .C++ class template specialization question
一个 C++ STL base type 的问题第五版的 c++ primer 出来了
template请教C++中的unordered_set
compile error新版的 eclipse 有什么大的改进?
where to define my template function关于C++ STL编译的疑问
template 类的继承问题C++ optimization question
C++ template questionstemplate metaprogramming 的问题
C++ templatethrust help ~~~
相关话题的讨论汇总
话题: elemtype话题: template话题: index话题: vec
进入Programming版参与讨论
1 (共1页)
w******g
发帖数: 67
1
I have a simple C++template code, but I cannot get it run.
In DataTemplate.h:
template
ElemType* getVecElemPointer(int index, vector& vec);
In DataTemplate.cpp:
template
ElemType* getVecElemPointer(int index, vector& vec)
{
if( (index0) )
{
return &vec[index];
}
return (ElemType*) 0;
}
It is fine when I compile it: "g++ -c -g DataTemplate.cpp"
In another class implementation file: test.cpp
#inclu
y*******g
发帖数: 6599
2
都放在头文件中呢?

【在 w******g 的大作中提到】
: I have a simple C++template code, but I cannot get it run.
: In DataTemplate.h:
: template
: ElemType* getVecElemPointer(int index, vector& vec);
: In DataTemplate.cpp:
: template
: ElemType* getVecElemPointer(int index, vector& vec)
: {
: if( (index0) )
: {

S*********g
发帖数: 5298
3
把cpp里的内容放到hpp里
另外,你这个getVecElemPointer不能返回第一个element啊

【在 w******g 的大作中提到】
: I have a simple C++template code, but I cannot get it run.
: In DataTemplate.h:
: template
: ElemType* getVecElemPointer(int index, vector& vec);
: In DataTemplate.cpp:
: template
: ElemType* getVecElemPointer(int index, vector& vec)
: {
: if( (index0) )
: {

w******g
发帖数: 67
4
Yes, it works when I put both the declaration and implementation in the head
file, like:
In DataTemplate.h:
template
ElemType* getVecElemPointer(int index, vector& vec)
{
if( (index0) )
{
return &vec[index];
}
return (ElemType*) 0;
}
The linkage works. Can you explain why? Thanks.
w******g
发帖数: 67
c*******3
发帖数: 21
6
Answer from http://www.cplusplus.com/doc/tutorial/templates.html:
Templates and multiple-file projects
From the point of view of the compiler, templates are not normal functions
or classes. They are
compiled on demand, meaning that the code of a template function is not
compiled until an
instantiation with specific template arguments is required. At that moment,
when an instantiation is
required, the compiler generates a function specifically for those arguments
from the template.
When projects

【在 w******g 的大作中提到】
: I have a simple C++template code, but I cannot get it run.
: In DataTemplate.h:
: template
: ElemType* getVecElemPointer(int index, vector& vec);
: In DataTemplate.cpp:
: template
: ElemType* getVecElemPointer(int index, vector& vec)
: {
: if( (index0) )
: {

1 (共1页)
进入Programming版参与讨论
相关主题
thrust help ~~~where to define my template function
boost::function 的 syntax 问题template 类的继承问题
[合集] 请问-fno-implicit-templates的用处C++ template questions
[合集] Effecive c++ 真那么好? (转载)C++ template
Help C++ Template function link error .C++ class template specialization question
一个 C++ STL base type 的问题第五版的 c++ primer 出来了
template请教C++中的unordered_set
compile error新版的 eclipse 有什么大的改进?
相关话题的讨论汇总
话题: elemtype话题: template话题: index话题: vec