【发布时间】:2011-11-02 01:39:58
【问题描述】:
我的模板化函数有两个模板参数(int m 和 int n)。该函数返回一个类Foo<int r>,我想要r=m+n。
在 foo.h 我有:
template<int m> class Foo {};
template<int m, int n>
Foo<m+n> myFunc(const Foo<m> &a, const Foo<n> &b);
在 foo.cpp 我有:
Foo<m+n> myFunc(const Foo<m> &a, const Foo<n> &b)
{
return Foo<m+n>();
}
最后在 main.cpp 中:
#include "foo.h"
int main()
{
myFunc(Foo<2>(), Foo<3>());
}
如果我尝试这个,我会得到一个链接器错误。:
"undefined reference to `Foo<(2)+(2)> myFunc(Foo<2> const&, Foo<2> const&)'
编辑:已编辑以包含完整的代码。也许不太清楚,但有些人更喜欢。
【问题讨论】: