P
Patrick Labatut
The following code should compile fine with g++ 3.3 and 3.4. However if
I instead put the friend operator* declaration right below the operator*
member, g++ 3.4 won't compile it anymore.
Could someone give me some explanation on this weird behaviour ?
/*************************************************************************/
template<class FloatT>
class Vector3;
template<class FloatT>
Vector3<FloatT> operator*(FloatT f, const Vector3<FloatT>& v);
template<class FloatT>
class Vector3 {
public:
Vector3(FloatT x, FloatT y, FloatT z)
{
this->v[0] = x;
this->v[1] = y;
this->v[2] = z;
}
friend Vector3<FloatT> operator*<>(FloatT f, const
Vector3<FloatT>& v);
FloatT operator*(const Vector3<FloatT>& v) const
{
return (this->v[0] * v.v[0]
+ this->v[1] * v.v[1]
+ this->v[2] * v.v[2]);
}
private:
FloatT v[3];
};
template<class FloatT>
Vector3<FloatT> operator*(FloatT f, const Vector3<FloatT>& v)
{
return Vector3<FloatT>(f * v.v[0], f * v.v[1], f * v.v[2]);
}
int main(int argc, char *argv[])
{
Vector3<float> v(0, 0, 0), w(0, 0, 0);
w = 2.0f * v;
return 0;
}
I instead put the friend operator* declaration right below the operator*
member, g++ 3.4 won't compile it anymore.
Could someone give me some explanation on this weird behaviour ?
/*************************************************************************/
template<class FloatT>
class Vector3;
template<class FloatT>
Vector3<FloatT> operator*(FloatT f, const Vector3<FloatT>& v);
template<class FloatT>
class Vector3 {
public:
Vector3(FloatT x, FloatT y, FloatT z)
{
this->v[0] = x;
this->v[1] = y;
this->v[2] = z;
}
friend Vector3<FloatT> operator*<>(FloatT f, const
Vector3<FloatT>& v);
FloatT operator*(const Vector3<FloatT>& v) const
{
return (this->v[0] * v.v[0]
+ this->v[1] * v.v[1]
+ this->v[2] * v.v[2]);
}
private:
FloatT v[3];
};
template<class FloatT>
Vector3<FloatT> operator*(FloatT f, const Vector3<FloatT>& v)
{
return Vector3<FloatT>(f * v.v[0], f * v.v[1], f * v.v[2]);
}
int main(int argc, char *argv[])
{
Vector3<float> v(0, 0, 0), w(0, 0, 0);
w = 2.0f * v;
return 0;
}