如何解决类模板-如何解决对“类对象”错误的未定义引用?
这是我的.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 举报,一经查实,本站将立刻删除。