返回信息流现有一个模板函数,声明是在head.h文件中
template <typename Comparable>
const Comparable &findMax(const vector<Comparable> &a);
实现实在findMax.cc中
template <typename Comparable>
const Comparable &findMax(const vector<Comparable> &a)
{
int maxIndex = 0;
for(int i=1; i<a.size(); i++)
{
if(a[maxIndex]<a[i])
maxIndex = i;
}
return a[maxIndex];
}
但是在main.c文件中调用findMax函数,编译时报错:
main.cc:(.text+0x2d5): undefined reference to `Employee const& findMax<Employee>(std::vector<Employee, std::allocator<Employee> > const&)'
出现这种错误是不是因为模板函数的声明和实现是不能分开的呢?
这是一条镜像帖。来源:北邮人论坛 / cpp / #19465同步于 2009/2/24
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
模板函数声明能和实现分开么?
lishuaigirl
2009/2/24镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复