我可以制作矢量对象的算术表达式吗?

can I make arithmetic expressions of vector objects?

对于 3 向量 class

template <typename T>
class vec3 {

   template <typename U>
   friend std::ostream& operator<<(std::ostream& out, const vec3<U>& v);

   template <typename U>
   friend std::istream& operator>>(std::istream& in, vec3<U>& v);

   protected:
   std::vector<T> data;

   public:
   vec3 (                    ): data(3)
        {}; // default constructor

   vec3 ( T x,   T y,   T z  ): data(3)
        { data[0] = x; data[1] = y; data[2] = z; } // constructor from 3 scalars

   vec3 ( T* xyz             ): data(3)
        { std::copy (xyz, xyz+3, data.begin() ); } // constructor from pointer

   vec3 ( const vec3<T>& rhs ): data(3)
        { std::copy ( rhs.data.begin(), rhs.data.end(), data.begin() ); } // copy constructor

   vec3<T> operator=( vec3<T> rhs ) {
        std::copy( rhs.data.begin(), rhs.data.end(), data.begin() ); // assignment    
        return (*this);
   }

   // passing on the [] operator for accessing elements
   T& operator[] ( size_t offset)       
         { return data[offset]; } // write access
   const T& operator[] ( size_t offset) const 
         { return data[offset]; } // read  access

   // left += and + for elements and vectors
   template <typename U>
   const vec3<T>& operator+= ( const U& rhs )       
         { data[0]+=rhs;    data[1]+=rhs;    data[2]+=rhs;    return (*this); }
   template <typename U>
   const vec3<T>& operator+= ( const vec3<U>& rhs ) 
         { data[0]+=rhs[0]; data[1]+=rhs[1]; data[2]+=rhs[2]; return (*this); }
   template <typename U>
   const vec3<T> operator+   ( const U& rhs )       
         { vec3<T> out(*this); out += rhs; return out;                        }

   // left *= and * for elements and vectors
   template <typename U>
   const vec3<T>& operator*= ( const U& rhs )       
         { data[0]*=rhs;    data[1]*=rhs;    data[2]*=rhs;    return (*this); }
   template <typename U>
   const vec3<T>& operator*= ( const vec3<U>& rhs ) 
         { data[0]*=rhs[0]; data[1]*=rhs[1]; data[2]*=rhs[2]; return (*this); }
   template <typename U>
   const vec3<T> operator*   ( const U& rhs )       
         { vec3<T> out(*this); out *= rhs; return out;                        }

   // rest of the operators

}

template <typename U>
std::ostream& operator<<(std::ostream& out, const vec3<U>& v)
   { return out << '(' << v.data[0] << ',' << v.data[1] << ',' << v.data[2] <<')'; }

template <typename U>
std::istream& operator>>(std::istream& in , vec3<U> &v)
   { return in >> v.data[0] >> v.data[1] >> v.data[2]; }

我试过运行这段代码

float v[3] = { 10, 20, 30 };

vec3<float> v1 (v);
std::cout << v1 << " \n";
vec3<float> v2 (v1);
std::cout << v2 << " \n";
vec3<float> v3 = v2;
std::cout << v3 << " \n \n";

v3+=1.;
std::cout << v3 << " \n";
v3=v1+v2*2;
std::cout << v3 << " \n";

哪个returns

(10,20,30) 
(10,20,30) 
(10,20,30) 

(11,21,31) 
Segmentation fault

所以我对复制构造函数、赋值 等有足够的信心 但是不可能将 * 操作的结果传递给 +?它 returns 一个 const vec3<T> 所以这里出了什么问题?我猜是一些菜鸟错误,但我没有看到。

v3=v1+v2*2;

这一行对函数进行递归调用

template <typename T, typename U>
inline vec3 <T> operator+ (U x, vec3 <T> y) {
    return y + x;
}

堆栈溢出后出现段错误。 如果去掉这个函数,成员函数const vec3<T> operator+ ( const U& rhs )会被调用运行,正常

您应该将您定义的全局运算符函数添加到您的问题中。