构造函数 (g++) 的显式模板专用化

Explicit template specialization for constructor (g++)

本文关键字:专用 g++ 构造函数      更新时间:2023-10-16

我在.h文件中有这个类定义:

class PolygonPath
{
public:
template<class T> explicit PolygonPath(const Polygon<T> &);
template<class T> Polygon<T> toPolygon() const;
}

.cpp文件中,我定义了我的方法。然后,我想为Polygon<float>Polygon<long>定义显式模板。所以,我这样定义它们:

template class PolygonPath::PolygonPath<float>(const Polygon<float> &); //Fail
template class Polygon<float> PolygonPath::toPolygon<float>() const; //Ok
template class PolygonPath::PolygonPath<long>(const Polygon<long> &); //Fail
template class Polygon<long> PolygonPath::toPolygon<long>() const; //Ok

对于构造函数,我无法定义显式模板专用化。我在编译时遇到此错误:">错误:'多边形路径'不是类模板"。 我也尝试使用这种语法:

template <> PolygonPath::PolygonPath(const Polygon<float> &)

它可以编译,但我在链接中收到另一个错误:"未定义对'海胆::P olygonPath::P olygonPath(urchin::P olygon const&('的引用"。

从构造函数的显式实例化中删除class

template PolygonPath::PolygonPath<long>(const Polygon<long> &);

template Polygon<long> PolygonPath::toPolygon<long>() const;