标签:c templates visual-c c17 explicit-instantiation
编辑:这不是链接问题的重复,因为我使用的是显式实例化,只有特定类型的成员函数不链接(其他人则链接).
以下代码可以编译,但无法链接,我也不明白为什么.
显式实例化Vector类以限制T的可能参数的数量,因此隐藏了Vector的定义.在.cpp文件中.
// fwd_decl.hpp
#pragma once
template<typename T>
struct Vector; // Forward declare Vector to be used in other headers
// Vector.hpp
#pragma once
#include "fwd_decl.hpp"
template<typename T>
struct Vector
{
template<typename U> // To allow for other types than T to be used
Vector operator+(const Vector<U> & other) const;
T x;
T y;
// more stuff..
};
// Vector.cpp
#include "Vector.hpp"
template<typename T>
template<typename U>
Vector<T> Vector<T>::operator+(const Vector<U> & other) const
{
return { static_cast<T>(x + other.x), static_cast<T>(y + other.y) };
}
template struct Vector<int>; // Explicitly instantiate Vector<T> with int
// main.cpp
#include "Vector.hpp"
int main()
{
Vector<int> b = Vector<int>{ 2, 3 } + Vector<int>{ 4, 5 };
}
我得到的错误是:
1>main.obj : error LNK2001: unresolved external symbol "public: struct Vector<int> __thiscall Vector<int>::operator+<int>(struct Vector<int> const &)const " (??$?HH@?$Vector@H@@QBE?AU0@ABU0@@Z)
我正在VS 15.9.4中使用VC 17进行编译.
注意,对Vector< int>成员的调用不是功能模板的链接可以正常链接.
解决方法:
您应该使用方法template< typename T>的显式实例. template< typename U>矢量< T>除了对向量< T>的显式实例化之外,向量< T> ::运算符(const Vector< U& other)const(对于T和U的所有可能的对).类:
template Vector<int> Vector<int>::operator+(const Vector<short> & other) const;
同样,您可以简单地将Vector< T> :: operator方法的定义移至头文件.
在C 11中,引入了extern template指令.您可以在Vector< T>的头文件中使用它.类别(如@StoryTeller suggested in the comments):
extern template struct Vector<int>;
…以防止编译器实例化Vector T.每个翻译单元中的class都使用其专业知识.当然,相同的extern模板指令也可以用于在.cpp文件中显式实例化的所有Vector T :: operator专业化.
标签:c,templates,visual-c,c17,explicit-instantiation 来源: https://codeday.me/bug/20191013/1910168.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。