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 |
|
w******g 发帖数: 67 | 2 Thanks a lot. Another question: if I want to define a function template to
print out the elements of the MapOfVec.
template
struct MapOfVec
{
typedef vector VecType;
typedef typename vector::iterator VecIter;
typedef map MapType;
typedef typename map::iterator MapIter;
};
template
void printElems_MapOfVec(const MapOfVec::MapType& map);
It a |
|
w******g 发帖数: 67 | 3 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. |
|
G****A 发帖数: 4160 | 4 下面这段程序编译没有错误,执行后windows却弹出错误提示框,请帮我看看怎么回事.谢谢
template
void display(const string &msg, const vector &vec)
{
elemType t = vec[0];
cout<
cout<
}
int main()
{
string a = "Hello";
vector ivec;
ivec[0] = 7;
display(a, ivec);
return 0;
} |
|
B********e 发帖数: 1062 | 5 template
struct MapofVec{
typedef vector VecType;
typedef typename vector::iterator VecIter;
typedef map MapType;
typedef typename map::iterator MapIter;
};
use
** |
|
B********e 发帖数: 1062 | 6 This may work.
template
class MapOfVec{
public:
typedef vector VecType;
typedef typename vector::iterator VecIter;
typedef map MapType;
typedef typename map::iterator MapIter;
void print(MapType& map);
};
agian. |
|
w******g 发帖数: 67 | 7 I have a question about template typedef. The question is that I want to use
the data structure like map>.
So I define the following:
#ifndef DATATEMPLATE_H_
#define DATATEMPLATE_H_
#include |
|