Struct成员函数中的

Struct成员函数中的

我试图让教授的过度设计的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()的行上出现错误。无论哪种方法首先出现在结构中,这似乎都会产生错误。有什么建议吗?

最佳答案

您是否正在尝试使用C编译器进行编译?如果您尝试使用仅C的编译器来编译C++代码,我想您会得到这些类型的消息。

关于c++ - “expected ' : ', ' , ', '; ', '} ' or ' __attribute__ ' before ' {Struct成员函数中的' token”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4247615/

10-12 14:44