【问题标题】:Visual Studio 2010 doesn't like typedef typename in mixinVisual Studio 2010 不喜欢 mixin 中的 typedef typename
【发布时间】:2015-12-23 06:18:18
【问题描述】:

我有一些代码可以用 gcc 很好地编译,但 Visual Studio 不喜欢。我把它提炼成一个最小的例子:

#include "stdafx.h"

#include <Eigen/Dense>

template<class Base>
class Timestamp : public Base
{
public:
    typedef typename Base::PointType PointType;
    double timestamp;
};

/*
struct Point {};

struct PointXYZ : public Point
{
    typedef PointXYZ PointType;
};
*/

struct PointXYZ : public Eigen::Vector3d
{
    typedef PointXYZ PointType;
};

int _tmain(int argc, _TCHAR* argv[])
{
    Timestamp<PointXYZ> point;
    return 0;
}

错误是"error C2039: 'PointType' : is not a member of 'Eigen::PlainObjectBase&lt;Derived&gt;'"

PlainObjectBase 类是 Eigen 库的一部分。如果我将 PointXYZ 的定义替换为 cmets 中派生自一个空“Point”类的定义,它在 VS 中也可以正常编译。关于为什么会发生这种情况以及 VS 可以改变什么以像 gcc 那样接受它的任何建议?

【问题讨论】:

    标签: c++ visual-studio-2010


    【解决方案1】:

    您在Timestamp 中重新定义类型成员PointType 有什么原因吗?您已经从您希望包含PointType 类型成员的类型继承,这意味着Timestamp 也通过继承具有PointType 类型成员。如果你从Timestamp 中去掉这个typedef,你就不应该有这个问题。

    我不熟悉 Eigen,但我想它是 this library。如果是这样,文档显示Vector3dMatrix 的typedef(即Vector3d 只是Matrix),它本身包含一个名为Base 的typedef 代表基班级PlainObjectBase&lt;Matrix&gt;:

    typedef PlainObjectBase<Matrix> Base;
    

    我的猜测是当你这样做时:

    typedef typename Base::PointType PointType;
    

    Base 被评估为 Matrix'Base typedef 而不是模板类型参数Base,因此它实际上是在尝试执行您的错误消息中所说的:

    typedef typename PlainObjectBase<Derived>::PointType PointType;
    

    这显然失败了,因为 PlainObjectBase 不包含 PointType 类型成员,更不用说这不是你想要做的。

    例如将Timestamp 的模板类型参数更改为B 是否可以解决问题?

    template<class B>
    class Timestamp : public B
    {
    public:
        typedef typename B::PointType PointType;
        double timestamp;
    };
    

    这将确认这是问题所在,并且可能确实是 Visual Studio 2010 中的一个错误(如果可以,请尝试更高版本)。就个人而言,尽管我建议您简单地删除 typedef,因为您已经继承了它。

    【讨论】:

    • 你在这两个方面都是对的。我更改了名称,并且有效。然后我删除了typedef,它仍然有效。谢谢!
    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2018-03-14
    相关资源
    最近更新 更多