使用Visual Studio中另一个项目的显式实例化模板函数

时间:2014-02-21 14:19:29

标签: c++ visual-studio templates visual-studio-2008

我在Visual Studio 2008的一个解决方案中有两个项目ProjA和ProjB.ProjA依赖于ProjB中的东西。 ProjB只包含头文件,因此不需要构建任何文件,也不存在库。 ProjB在标头TemplB.h中定义模板函数templB。 ProjA使用该模板函数作为templB。到目前为止一切正常。但是,当我在TemplB.h中明确地实例化templB时,我得到一个链接器错误LNK2001,告诉我templB是一个未解析的外部符号。

问题出在哪里? VS是否期望明确的即时模板在ProjB中的某个目标文件或库中,即使ProjB中没有任何目标文件,也必须链接到该文件或库中。

感谢。

我在这里放了两个最小的测试项目:https://dl.dropboxusercontent.com/u/16089481/Projects.zip

1 个答案:

答案 0 :(得分:0)

我认为你使用声明而不是 instantiation ......

template <int N> int id() {return N;}

template<> int id<2>(); // just declare id<2> specialisation: definition needed.
template int id<3>();   // Explicit instantiation of id<3>.