简体   繁体   English

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

[英]Conversion operator from one templated class to another, related template class

Did search, but couldn't find something that matches my query, it's a bit specific, so here goes. 进行了搜索,但是找不到与我的查询相匹配的内容,它有点具体,所以这里去。

I have some templated classes (Vector2, Vector3, and Vector4). 我有一些模板化类(Vector2,Vector3和Vector4)。 Trying to define a conversion operator from Vector2 to 3 and 4, and Vector3 to 2 and 4, etc. 尝试定义从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;
    // ...
}

Using Visual Studio 2017 gives me this: 使用Visual Studio 2017可为我提供以下信息:

error C2833: 'operator Vector2' is not a recognized operator or type 错误C2833:“运算符Vector2”不是公认的运算符或类型

Any, and all help is appreciated. 任何,所有帮助表示赞赏。

Thank you. 谢谢。

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. 编辑:现在我得到错误C2988:无法识别的模板声明/定义。 It's probably worth mentioning that the 3 template classes are in separate files. 值得一提的是,这3个模板类位于单独的文件中。 I originally tried including a header just in one class to get the type conversion operator working, this is what was giving original errors. 我最初试图在一个类中包含一个标头,以使类型转换运算符正常工作,这就是产生原始错误的原因。

Oh, yes. 哦是的 I will definitely be making those explicit. 我一定会明确指出这些。 That's always good advice. 总是好的建议。 It was 0430 localtime though... :) 虽然是当地时间0430 ... :)

Edit: Nevermind, I'm a spaz. 编辑:没关系,我是个spaz。 I don't know how I slipped a space in between Vector and the number of dims "Vector 2" != "Vector2". 我不知道如何在Vector和暗号“ 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. 孩子们:当您如此受束缚时不要编码,这太好了。

When you declare Vector4<T>::operator Vector2<T>() const; 当您声明Vector4<T>::operator Vector2<T>() const; you are using the class Vector2<T> before it's been declared. 在声明Vector2<T>之前,您正在使用它。 The same thing happens for Vector4<T>::operator Vector3<T>() const; Vector4<T>::operator Vector3<T>() const;发生相同的情况Vector4<T>::operator Vector3<T>() const; . Forward declare your classes first. 先声明您的课程。

// 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;
    // ...
};

You have a circular dependency. 您具有循环依赖关系。
You can resolve it by using converting constructors in one "direction". 您可以通过在一个“方向”上转换构造函数来解决它。
This one uses constructors for increasing the dimensions, conversion operators for decreasing them: 这个使用构造函数来增加尺寸,使用转换运算符来减小尺寸:

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;
};

(Insert stern warning about the perils of implicit conversions here.) (在此处插入有关隐式转换的危险的严厉警告。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM