gpt4 book ai didi

c++ - 我不明白这个链接错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:53:31 28 4
gpt4 key购买 nike

我不明白这个链接错误。我有 2 个类(class):

#include "Vector3.h"
#include "Quaternion.h"

template<typename T>
class Point3 final
{
public:
constexpr Point3(const Vector3<T>& vec)
: x(vec.x), y(vec.y), z(vec.z)
{}

constexpr operator const Vector3<T>() const
{
// It is the equivalent of Vector3 = Point3 - Origin
return Vector3<T>(x, y, z);
}

constexpr operator Vector3<T>() const
{
// It is the equivalent of Vector3 = Point3 - Origin
return Vector3<T>(x, y, z);
}

T x = T(0);
T y = T(0);
T z = T(0);

friend Vector3<T>;
friend Quaternion<T>;

friend Vector3<T> operator*( const Quaternion<T>& lhs, const Vector3<T>& rhs);
friend Vector3<T> operator*( Vector3<T> lhs, const Vector3<T>& rhs);
};

typedef Point3<Float32> Point3f;

template<typename T>
class Vector3 final
{
public:

constexpr Vector3()
{}

constexpr Vector3(T _x, T _y, T _z)
: x(_x), y(_y), z(_z)
{}

T x = T(0);
T y = T(0);
T z = T(0);

};

typedef Vector3<Float32> Vector3f;

我也有一个四元数类,我相信细节无关紧要,但是这个类有一个非成员运算符*:

 template<typename T>
Vector3<T> operator*( const Quaternion<T>& lhs, const Vector3<T>& rhs)
{
// nVidia SDK implementation
Vector3<T> qvec(lhs.x, lhs.y, lhs.z);
Vector3<T> uv = cross(qvec, rhs) * T(2.0) * lhs.w; //uv = qvec ^ v;
Vector3<T> uuv = cross(qvec, uv) * T(2.0); //uuv = qvec ^ uv;
return rhs + uv + uuv;
}

现在这些行产生链接错误,但为什么呢?

Math::Point3<Float32> pt = -Math::Point3<Float32>::UNIT_Z;
Math::Vector3<Float32> vec = orientation_*pt; // link error here (orientation is a Quaternion<Float32>)
//Math::Vector3<Float32> vec = orientation_*Math::Vector3<Float32>(pt); // this solve the link error.

这里是链接错误

Undefined symbols for architecture x86_64:
Math::operator*(Math::Quaternion<float> const&, Math::Vector3<float> const&), referenced from:
GfxObject::Procedural::BoxGenerator::addToTriangleBuffer(GfxObject::Procedural::TriangleBuffer&) const in ProceduralBoxGenerator.o

更新

我发现有 2 个问题与此非常接近,但问题在于差异。

在: question 1question 2

但在我的例子中,我需要在 2 个模板类之间进行转换,而不是在同一个类和 2 个实例之间进行转换。我希望这会有所帮助!

最佳答案

尝试确保编译器知道您的友元声明应该是模板特化,而不是全新的非模板函数的声明:

friend Vector3<T> operator* <> (const Quaternion<T>& lhs, const Vector3<T>& rhs);

C++ 常见问题解答中讨论了这个常见错误 here .

关于c++ - 我不明白这个链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17078262/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com