类模板-如何解决对“类对象”错误的未定义引用?

问题描述

这是我的.h文件中的代码(如下所示)。

//Scalar
    Vector<T> operator*(const T &d) const { return Vector<T>(x_pos * d,y_pos * d,z_pos * d); };
    //Multiplying Vectors
    Vector<T> operator*(const Vector<T> &v) const { return Vector<T>(x_pos * v.x_pos,y_pos * v.y_pos,z_pos * v.z_pos); };
    Vector<T> operator/(const T &d) const { return Vector<T>(x_pos / d,y_pos / d,z_pos / d); }
    Vector<T> operator+(const Vector<T> &v) const { return Vector<T>(x_pos + v.x_pos,y_pos + v.y_pos,z_pos + v.z_pos); }
    Vector<T> operator-(const Vector<T> &v) const { return Vector<T>(x_pos - v.x_pos,y_pos - v.y_pos,z_pos - v.z_pos); }
    //Formula
    T dot_product(Vector<T> a);
    Vector<T> cross_product(Vector<T> a);
    //2X2 Matrix
    T constexpr magnitude();
    Vector<T> &normalize();

我在下面的cc文件中这样称呼它。

  template <typename T>
T raycast::Vector<T>::dot_product(Vector<T> a)
{
    return get_x() * a.get_x() + get_y * a.get_y + get_z * a.get_z;
}

template <typename T>
raycast::Vector<T> raycast::Vector<T>::cross_product(Vector<T> a)
{
    return Vector<T>((get_y() * a.get_z() - get_z() * a.get_y()),(get_z * a.get_x - get_x * a.get_z),(get_x * a.get_y - get_y * a.get_x));
}

template <typename T>
T constexpr raycast::Vector<T>::magnitude()
{
    return sqrt(get_x * get_x + get_y * get_y + get_z * get_z);
}

template <typename T>

raycast::Vector<T> &raycast::Vector<T>::normalize()
{
    T inverse = 1 / magnitude();
    x_pos *= inverse;
    y_pos *= inverse;
    z_pos *= inverse;
    return *this;
}

例如,我将在我的代码中收到此错误

undefined reference to `raycast::Vector<double>::dot_product(raycast::Vector<double>)'
collect2.exe: error: ld returned 1 exit status.

是因为我没有在cc文件中正确调用它,还是因为我没有在cc文件调用构造函数?我的文件或makefile的路径没有问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)