我可以对矢量对象进行算术表达式吗?

问题描述 投票:0回答:1

对于3-向量类

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";

返回]

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

(11,21,31) 
Segmentation fault

所以我对复制构造函数,赋值

很有信心,但是不可能将*操作的结果传递给+吗?它返回const vec3<T>,这是怎么回事?我猜是一些菜鸟错误,但我看不到。

对于3矢量类模板vec3类{模板朋友std :: ostream&运算符

我将您的代码复制到了https://www.onlinegdb.com/online_c++_compiler,并且可以正常工作。

我认为您的程序中正在运行更多代码。

可能是段错误在std :: cout << v3 <

c++ segmentation-fault operators pass-by-reference pass-by-value
1个回答
0
投票

我将您的代码复制到了https://www.onlinegdb.com/online_c++_compiler,并且可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.