对从模板派生的类构造函数的未定义引用

时间:2014-09-20 08:23:06

标签: c++ templates constructor

我有一个名为Interpolator的模板类。

Interpolator.hpp

namespace interpolation {
    template< class Abscissa >
    class Interpolator;
}

template< class Abscissa >
class interpolation::Interpolator {

public:

    Interpolator();

    virtual
    ~Interpolator();

};

Interpolator.cpp

template< class Abscissa >
interpolation::Interpolator< Abscissa >::Interpolator()
{
}

template< class Abscissa >
interpolation::Interpolator< Abscissa >::~Interpolator()
{
}

然后,我有一个特定插值形状的类。形状是四边形,由Abscissa_Quad类处理。这种形状的插值由Interpolator_Quad类处理。

Interpolator_Quad.hpp

namespace interpolation {
    class Abscissa_Quad;
    class Interpolator_Quad;
}

class interpolation::Interpolator_Quad 
    : public interpolation::Interpolator< interpolation::Abscissa_Quad > {

public:

    Interpolator_Quad();

    ~Interpolator_Quad();

};

在.cpp文件中我有:

Interpolator_Quad.cpp

interpolation::Interpolator_Quad::Interpolator_Quad()
    : Interpolator()
{
}

interpolation::Interpolator_Quad::~Interpolator_Quad()
{
}

最后,我使用main.cpp文件中的类,如下所示:

的main.cpp

#include "interpolation/Interpolator_Quad.hpp"
#include "interpolation/Interpolator.hpp"
#include "integration/Abscissa_Quad.hpp"

int main(
        int    argc,
        char * argv[] )
{
    interpolation::Interpolator_Quad interpolator;

    // Do some stuff with the interpolator.
}

但现在我一直收到这个错误:

undefined reference to `interpolation::Interpolator<interpolation::Abscissa_Quad>::Interpolator()'

我尝试过不同的方法,主要来自这些问题:

  • "Undefined reference to" template class constructor 我尝试在main.cpp中使用:  插值:: Interpolator_Quad&LT; interpolation :: Abscissa_Quad&gt;内插器; 但当然这不起作用,因为Interpolator_Quad不是模板。
  • 从这个问题: undefined reference to c++ class constructor 我知道模板的实现必须在头文件中。我不明白为什么编译器找不到对模板构造函数的引用,因为Interpolator_Quad在头文件中将模板类定义为Abscissa_Quad。

我理解在提到模板构造函数的未定义引用之前已经提出了类似的问题;然而,我发现的例子要么不起作用,要么我完全不理解它们。

对不起,我对我的问题中的命名空间感到有点疯狂。我只是想确保我的错误不在于它们的不当使用。

我正在使用gcc 4.8.2和Ubuntu机器。

感谢任何反馈。谢谢!

0 个答案:

没有答案
相关问题