M
mast2as
Hello, I have 2 classes *as an example* Point & Color and friend
operator * in the class Point is defined in such a way that it is
possible to do something like that Point p = Point( 1.0 ) * Color( 2.0
);
// in point.hpp
class Color;
class Point {
public:
float x, r, z;
Point( float xx ) : x( xx ), y( xx ), z( xx ) {}
friend Point operator * ( const Point &p, const Color &c )
{ return Point( p.x * c.x ); }
};
//in color.hpp
class Vector;
class Color {
public:
float r, g, b;
Point( float xx ) : r( xx ), g( xx ), b( xx ) {}
};
// so that line somwhere in the code works
Point p = Point( 1.0 ) * Color( 2.0 );
And it compile fine.
Now I tried to provide the Point vector with a constuctor where the
argument that is passed is a Color and rewrote the friend operator * to
take advantage of that:
class Point {
...
Point( const Color& c ) : x( c.r ), y( c.g ), z( c.b ) {}
Point operator * ( const Point& p ) { return Point( x * p.x, y * p.y,
z * p.z ); }
friend Point operator * ( const Point &p, const Color &c )
{ return p * Point( c ); }
};
But when I compile I get the following error message:
error: ISO C++ says that these are ambiguous, even though the worst
conversion for the first is better than the worst conversion for the
second:
../math/point.hpp:90: note: candidate 1: Point<float> operator*(const
Point<float>&, const Color<float>&)
../math/point.hpp:55: note: candidate 2: Point<T>
Point<T>:perator*(const Point<T>&) [with T = float]
Basically the compile from what I understand the compiler doesn't know
if it needs to convert the color into a point and use the * operator or
to use the friend * operator and keep the right hand side argument as a
Color.
Could someone tell me what is the best way of doing that please ? Just
using constructors that allows explicit conversion of different types (
Color to Point ) and use the operator of the Point class, or not having
any explicit conversion in the Point class and only use the friend * (
Point, Color ) operator.
Thanks -
operator * in the class Point is defined in such a way that it is
possible to do something like that Point p = Point( 1.0 ) * Color( 2.0
);
// in point.hpp
class Color;
class Point {
public:
float x, r, z;
Point( float xx ) : x( xx ), y( xx ), z( xx ) {}
friend Point operator * ( const Point &p, const Color &c )
{ return Point( p.x * c.x ); }
};
//in color.hpp
class Vector;
class Color {
public:
float r, g, b;
Point( float xx ) : r( xx ), g( xx ), b( xx ) {}
};
// so that line somwhere in the code works
Point p = Point( 1.0 ) * Color( 2.0 );
And it compile fine.
Now I tried to provide the Point vector with a constuctor where the
argument that is passed is a Color and rewrote the friend operator * to
take advantage of that:
class Point {
...
Point( const Color& c ) : x( c.r ), y( c.g ), z( c.b ) {}
Point operator * ( const Point& p ) { return Point( x * p.x, y * p.y,
z * p.z ); }
friend Point operator * ( const Point &p, const Color &c )
{ return p * Point( c ); }
};
But when I compile I get the following error message:
error: ISO C++ says that these are ambiguous, even though the worst
conversion for the first is better than the worst conversion for the
second:
../math/point.hpp:90: note: candidate 1: Point<float> operator*(const
Point<float>&, const Color<float>&)
../math/point.hpp:55: note: candidate 2: Point<T>
Point<T>:perator*(const Point<T>&) [with T = float]
Basically the compile from what I understand the compiler doesn't know
if it needs to convert the color into a point and use the * operator or
to use the friend * operator and keep the right hand side argument as a
Color.
Could someone tell me what is the best way of doing that please ? Just
using constructors that allows explicit conversion of different types (
Color to Point ) and use the operator of the Point class, or not having
any explicit conversion in the Point class and only use the friend * (
Point, Color ) operator.
Thanks -