c++ - 我不明白VS2013编译器显示的错误

标签 c++ templates migration

以下是我尝试使用 Visual Studio 2013 编译的类。代码最初是在 Visual Studio 2005 中编写的。它是一个 project from github .显然,代码可以在 Visual Studio 2005 中编译,但在 Visual Studio 2013 中会出现语法错误。语言规范是否已更改?或者这是向后兼容的问题?

template<class Real = double>
class Quaternion //normalized quaternion for representing rotations
{
public:
    //constructors
    Quaternion() : r(1.) { } //initialize to identity
    Quaternion(const Quaternion &q) : r(q.r), v(q.v) {} //copy constructor
    template<class R> Quaternion(const Quaternion<R> &q) : r(q.r), v(q.v) {} //convert quaternions of other types
    //axis angle constructor:
    template<class R> Quaternion(const Vector<R, 3> &axis, const R &angle) : r(cos(angle * Real(0.5))), v(sin(angle * Real(0.5)) * axis.normalize()) {}
    //minimum rotation constructor:
    template<class R> Quaternion(const Vector<R, 3> &from, const Vector<R, 3> &to) : r(1.)
    {
        R fromLenSq = from.lengthsq(), toLenSq = to.lengthsq();
        if(fromLenSq < toLenSq) {
            if(fromLenSq < R(1e-16))
                return;
            Vector<R, 3> mid = from * sqrt(toLenSq / fromLenSq) + to;
            R fac = 1. / sqrt(mid.lengthsq() * toLenSq);
            r = (mid * to) * fac;
            v = (mid % to) * fac;
        }
        else {
            if(toLenSq < R(1e-16))
                return;
            Vector<R, 3> mid = from + to * sqrt(fromLenSq / toLenSq);
            R fac = 1. / sqrt(mid.lengthsq() * fromLenSq);
            r = (from * mid) * fac;
            v = (from % mid) * fac;
        }
    }

    //quaternion multiplication
    Quaternion operator*(const Quaternion &q) const { return Quaternion(r * q.r - v * q.v, r * q.v + q.r * v + v % q.v); }

    //transforming a vector
    Vector<Real, 3> operator*(const Vector<Real, 3> &p) const
    {
        Vector<Real, 3> v2 = v + v;
        Vector<Real, 3> vsq2 = v.apply(multiplies<Real>(), v2);
        Vector<Real, 3> rv2 = r * v2;
        Vector<Real, 3> vv2(v[1] * v2[2], v[0] * v2[2], v[0] * v2[1]);
        return Vector<Real, 3>(p[0] * (Real(1.) - vsq2[1] - vsq2[2]) + p[1] * (vv2[2] - rv2[2]) + p[2] * (vv2[1] + rv2[1]),
                               p[1] * (Real(1.) - vsq2[2] - vsq2[0]) + p[2] * (vv2[0] - rv2[0]) + p[0] * (vv2[2] + rv2[2]),
                               p[2] * (Real(1.) - vsq2[0] - vsq2[1]) + p[0] * (vv2[1] - rv2[1]) + p[1] * (vv2[0] + rv2[0]));
    }

    //equality
    template<class R> bool operator==(const Quaternion<R> &oth) const
    {
        return (r == oth.r && v == oth.v) || (r == -oth.r && v == -oth.v);
    }

    Quaternion inverse() const { return Quaternion(-r, v); }

    Real getAngle() const { return Real(2.) * atan2(v.length(), r); }
    Vector<Real, 3> getAxis() const { return v.normalize(); }

    const Real &operator[](int i) const { return (i == 0) ? r : v[i - 1]; }
    void set(const Real &inR, const Vector<Real, 3> &inV) {
        Real ratio = Real(1.) / sqrt(inR * inR + inV.lengthsq()); 
        r = inR * ratio; v = inV * ratio; //normalize
    }

private:
    Quaternion(const Real &inR, const Vector<Real, 3> &inV) : r(inR), v(inV) {}

    Real r;
    Vector<Real, 3> v;
};

显示的错误是:

Error   1   error C2989: 'Quaternion' : class template has already been declared as a non-class template    c:\users\student\documents\visual studio 2013\projects\narovatar\imp_pinocchio\transform.h  93  1   Rigging

Error   3   error C3857: 'Quaternion': multiple template parameter lists are not allowed    c:\users\student\documents\visual studio 2013\projects\narovatar\imp_pinocchio\transform.h  24  1   Rigging

谁能告诉我这个错误是什么意思???

最佳答案

这意味着在其他地方已经有一个名为 Quaternion 的非模板类(在本例中,它看起来像是在 transform.h 文件中)。如果这是项目中包含的文件之一,那么它可能是语言规范不再允许的类的前向声明。您必须自己编辑 header 才能解决问题。

关于c++ - 我不明白VS2013编译器显示的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34217478/

相关文章:

c++ - typedef struct name 没有后续结构定义的名称

c++ - 使用专门的模板根据返回类型重载函数

templates - 玩!框架从 Controller 调用 render() 操作

MySQL 根据另一个表的结果批量更新新表

git - svn 到 git 迁移 : incomplete history

c++ - system() 函数不工作 C++

c++ - 如何让 Netbeans 从项目中删除已删除的文件?

c++ - boost::gregorian input_facet 意外结果

c++ - 多个类型名的部分模板特化

sql-server - 任何将 SQLite 数据库转换为 sql server 的工具?