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

can I make arithmetic expressions of vector objects?

本文关键字:表达式 对象 我可以      更新时间:2023-10-16

对于 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>那么这里出了什么问题?我猜是一些菜鸟的错误,但我没有看到。

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 )进行操作,并且它工作正常。

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