从一个模板化 class 到另一个相关模板 class 的转换运算符

Conversion operator from one templated class to another, related template class

进行了搜索,但找不到与我的查询相匹配的内容,它有点具体,所以就这样吧。

我有一些模板 类(Vector2、Vector3 和 Vector4)。 尝试定义从 Vector2 到 3 和 4 的转换运算符, 和 Vector3 到 2 和 4,等等

template <typename T>
class Vector4 {
    // ...
    operator Vector2<T>() const { return { x, y }; }
    operator Vector3<T>() const { return { x, y, z }; }
    // ...
    T x, y, z, w;
    // ...
}


template <typename T>
class Vector3 {
    // ...
    operator Vector2<T>() const { return { x, y }; }
    operator Vector4<T>() const { return { x, y, z, 0 }; }
    // ...
    T x, y, z;
    // ...
}


template <typename T>
class Vector2 {
    // ...
    operator Vector3<T>() const { return { x, y, 0 }; }
    operator Vector4<T>() const { return { x, y, 0, 0 }; }
    // ...
    T x, y;
    // ...
}

使用 Visual Studio 2017 给我这个:

error C2833: 'operator Vector2' is not a recognized operator or type

感谢任何帮助。

谢谢。

Edit: My actual source does have semi-colons after class defs. Forgot to put them in the brief version I posted. Also, yes, there were many errors, but in my experience, it's usually the first one that matters Tried forward declaring:

template <class T> class Vector 3;
template <class T> class Vector 4;

template <typename T> 
class Vector2 {
// ...
}

Edit: Now I get error C2988: unrecognizable template declaration/definition. It's probably worth mentioning that the 3 template classes are in separate files. I originally tried including a header just in one class to get the type conversion operator working, this is what was giving original errors.

哦,是的。我一定会把这些说清楚。这总是好的建议。虽然是当地时间 0430...:)

Edit: Nevermind, I'm a spaz. I don't know how I slipped a space in between Vector and the number of dims "Vector 2" != "Vector2". Forward declaration it was. Can't believe I missed something so simple. Kids: Don't code when your so tied, itsa noso good.

当您声明 Vector4<T>::operator Vector2<T>() const; 时,您正在使用 class Vector2<T> 之前的声明。 Vector4<T>::operator Vector3<T>() const; 也会发生同样的事情。先转发声明你的 classes。

// Forward declarations
template<class T> class Vector2;
template<class T> class Vector3;

template <typename T>
class Vector4 {
    // ...
    operator Vector2<T>() const { return{ x, y }; }
    operator Vector3<T>() const { return{ x, y, z }; }
    // ...
    T x, y, z, w;
    // ...
};


template <typename T>
class Vector3 {
    // ...
    operator Vector2<T>() const { return{ x, y }; }
    operator Vector4<T>() const { return{ x, y, z, 0 }; }
    // ...
    T x, y, z;
    // ...
};


template <typename T>
class Vector2 {
    // ...
    operator Vector3<T>() const { return{ x, y, 0 }; }
    operator Vector4<T>() const { return{ x, y, 0, 0 }; }
    // ...
    T x, y;
    // ...
};

你有循环依赖。
您可以通过在 "direction".
中使用转换构造函数来解决它 这个使用构造函数来增加维度,使用转换运算符来减少维度:

template <typename T>
class Vector2 {
    T x, y;
};

template <typename T>
class Vector3 {
    Vector3(const Vector2<T>& v2) : x(v2.x), y(v2.y), z(0) {}
    operator Vector2<T>() const { return { x, y }; }
    T x, y, z;
};

template <typename T>
class Vector4 {
    Vector4(const Vector2<T>& v2) : x(v2.x), y(v2.y), z(0), w(0) {}
    Vector4(const Vector3<T>& v3) : x(v3.x), y(v3.y), z(v3.z), w(0) {}
    operator Vector2<T>() const { return { x, y }; }
    operator Vector3<T>() const { return { x, y, z }; }
    T x, y, z, w;
};

(在此插入关于隐式转换危险的严厉警告。)