Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用visual studio中另一个项目的显式实例化模板函数_C++_Visual Studio_Templates_Visual Studio 2008 - Fatal编程技术网

C++ 使用visual studio中另一个项目的显式实例化模板函数

C++ 使用visual studio中另一个项目的显式实例化模板函数,c++,visual-studio,templates,visual-studio-2008,C++,Visual Studio,Templates,Visual Studio 2008,我在VisualStudio2008的一个解决方案中有两个项目ProjA和ProjB。ProjA依赖于ProjB中的内容。ProjB只包含标题,因此不需要构建任何内容,也不存在库。ProjB在头templab.h中定义模板函数templab。ProjA将该模板函数用作templab。到目前为止一切正常。然而,当我在templB.h中显式实例化templB时,我得到一个链接器错误LNK2001,告诉我templB是一个未解析的外部符号 问题在哪里?VS是否希望显式实例化的模板位于我必须链接的Pro

我在VisualStudio2008的一个解决方案中有两个项目ProjA和ProjB。ProjA依赖于ProjB中的内容。ProjB只包含标题,因此不需要构建任何内容,也不存在库。ProjB在头templab.h中定义模板函数templab。ProjA将该模板函数用作templab。到目前为止一切正常。然而,当我在templB.h中显式实例化templB时,我得到一个链接器错误LNK2001,告诉我templB是一个未解析的外部符号

问题在哪里?VS是否希望显式实例化的模板位于我必须链接的ProjB中的某个对象文件或库中,即使ProjB中没有任何对象文件

谢谢

我在这里放了两个最小的测试项目:

我认为您使用声明而不是实例化

template int id(){return N;}
模板int id();//只需声明id专业化:需要定义。
模板int id();//id的显式实例化。
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>.