代码之家  ›  专栏  ›  技术社区  ›  Ash Furrow

结构成员函数中“{”token“之前应为“:”、“,”、“;”、“}”或“\u attribute”

  •  4
  • Ash Furrow  · 技术社区  · 15 年前

    我试图让我教授的过度编程的C++代码编译。这是我的密码:

    /**
     * Vector class.
     * Common mathematical operations on vectors in R3.
     *
     * Written by Robert Osada, March 1999.
     **/
    #ifndef __VECTOR_H__
    #define __VECTOR_H__
    
    /**
     * Vector3
     **/
    struct Vector3f
    {
      // coordinates
      float x, y, z;
    
      // norm
      float      normSquared () { return x*x+y*y+z*z; }
      double norm        () { return sqrt(normSquared()); }
    
      // boolean operators
      bool operator == (const Vector3f& v) const { return x==v.x && y==v.y && z==v.z; }
      bool operator != (const Vector3f& v) const { return x!=v.x || y!=v.y || z!=v.z; }
    
      // operators
      Vector3f  operator +  (const Vector3f &v) const { return Vector3f(x+v.x, y+v.y, z+v.z); }
      Vector3f& operator += (const Vector3f &v)       { x+=v.x; y+=v.y; z+=v.z; return *this; }
      Vector3f  operator -  () const                 { return Vector3f(-x, -y, -z); }
      Vector3f  operator -  (const Vector3f &v) const { return Vector3f(x-v.x, y-v.y, z-v.z); }
      Vector3f& operator -= (const Vector3f &v)       { x-=v.x; y-=v.y; z-=v.z; return *this; }
      Vector3f  operator *  (float s) const              { return Vector3f(x*s, y*s, z*s); }
      Vector3f& operator *= (float s)                { x*=s; y*=s; z*=s; return *this; }
      Vector3f  operator /  (float s) const          { assert(s); return (*this)* (1/s); }
      Vector3f& operator /= (float s)                { assert(s); return (*this)*=(1/s); }
    
     // create a vector
     Vector3f (float x_=0, float y_=0, float z_=0) : x(x_), y(y_), z(z_) {};
    
     // set coordinates
     void set (float x_, float y_, float z_) { x=x_; y=y_; z=z_; }
    };
    
    inline float Dot (const Vector3f& l, const Vector3f r)
    {
      return l.x*r.x + l.y*r.y + l.z*r.z;
    }
    
    // cross product
    inline Vector3f Cross (const Vector3f& l, const Vector3f& r)
    {
      return Vector3f(
        l.y*r.z - l.z*r.y,
        l.z*r.x - l.x*r.z,
        l.x*r.y - l.y*r.x );
    }
    
    #include "Misc.h"
    /*
    inline Vector3f Min (const Vector3f &l, const Vector3f &r)
    {
      return Vector3f(Min(l.x,r.x), Min(l.y,r.y), Min(l.z,r.z));
    }
    
    inline Vector3f Max (const Vector3f &l, const Vector3f &r)
    {
      return Vector3f(Max(l.x,r.x), Max(l.y,r.y), Max(l.z,r.z));
    }
    */
    #endif
    

    我得到了定义normSquared()的行上的错误。不管结构中哪个方法是第一个,它似乎都会给出一个错误。有什么建议吗?

    3 回复  |  直到 15 年前
        1
  •  1
  •   onemasse    15 年前

    你想用C编译器编译吗?如果你试图用C++编译器编译你的C++代码,我想你会得到那些类型的消息。

        2
  •  1
  •   Mark B    15 年前

    内联函数缺少类作用域说明符:否 Vector3f:: .

    例如:

    inline float Vector3f::Dot (const Vector3f& l, const Vector3f r)
    {
        // ...
    }
    
        3
  •  0
  •   Ash Furrow    15 年前

    我无法解决编译器错误,所以我只是将这些方法重写为静态内联方法。通过从结构中取出所有东西,我设法使它工作起来。我刚刚删除的运算符重载是因为它们实际上在项目的其他任何地方都没有使用。(我说是过度设计了)。

    谢谢所有的回答!