A
alessio211734
I have a class that defines a generic point where p={a1..an}
template<class T,int i> class Point
{
public:
T v;
Point<T,i>()
{
for (int j=0;j<i;j++) v[j]=0;
}
inline T & operator [] ( const int j ){
assert(j>=0 && j<i);
return v[j];
}
inline const T & operator [] ( const int j ) const {
assert(j>=0 && j<i);
return v[j];
}
inline Point<T,i>& operator=(const Point<T,i> & num)
{
for (int j=0;j<i;j++) v[j]=num.v[j];
return *this;
}
inline Point<T,i>& operator=(const float* pnum)
{
for (int j=0;j<i;j++) v[j]=pnum[j];
return *this;
}
inline Point<T,i> operator + ( Point<T,i> const & num) const {
Point<T,i> vect;
for (int j=0;j<i;j++) vect.v[j]=v[j]+num.v[j];
return vect;
}
inline Point<T,i> operator - ( Point<T,i> const & num) const {
Point<T,i> vect;
for (int j=0;j<i;j++) vect.v[j]=v[j]-num.v[j];
return vect;
}
inline Point<T,i> operator * ( const T s ) const {
Point<T,i> vect;
for (int j=0;j<i;j++) vect.v[j]=v[j]*s;
return vect;
}
inline Point<T,i>& operator *= ( const T s ){
for (int j=0;j<i;j++) v[j]=v[j]*s;
return *this;
}
}
I define a new class Point2
template<class T> class Point2: public Point<T,2>
{
public:
Point2<T>()
{
v[0]=0;
v[1]=1;
}
Point2<T>(const T& a, const T& b)
{
v[0]=a;
v[1]=b;
}
T x() { return v[0] };
T y() { return v[1] };
};
and in main program I run:
Point2f a;
Point2f b;
Point2f c;
c=a+b;
I get this error:
error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'Point<T,i>' (or there is no acceptable conversion)
with
[
T=float,
i=2
]
could be 'Point2<T> &Point2<T>:perator =(const Point2<T>
&)'
with
[
T=float
]
while trying to match the argument list '(Point2f,
Point<T,i>)'
with
[
T=float,
i=2
]
Why Point2 don't call the operator= defined in Point<i,T>?
Point2 is a Point<i,T> because derive by Point<i,T>, I don't
understand...
Please explain me.
template<class T,int i> class Point
{
public:
T v;
Point<T,i>()
{
for (int j=0;j<i;j++) v[j]=0;
}
inline T & operator [] ( const int j ){
assert(j>=0 && j<i);
return v[j];
}
inline const T & operator [] ( const int j ) const {
assert(j>=0 && j<i);
return v[j];
}
inline Point<T,i>& operator=(const Point<T,i> & num)
{
for (int j=0;j<i;j++) v[j]=num.v[j];
return *this;
}
inline Point<T,i>& operator=(const float* pnum)
{
for (int j=0;j<i;j++) v[j]=pnum[j];
return *this;
}
inline Point<T,i> operator + ( Point<T,i> const & num) const {
Point<T,i> vect;
for (int j=0;j<i;j++) vect.v[j]=v[j]+num.v[j];
return vect;
}
inline Point<T,i> operator - ( Point<T,i> const & num) const {
Point<T,i> vect;
for (int j=0;j<i;j++) vect.v[j]=v[j]-num.v[j];
return vect;
}
inline Point<T,i> operator * ( const T s ) const {
Point<T,i> vect;
for (int j=0;j<i;j++) vect.v[j]=v[j]*s;
return vect;
}
inline Point<T,i>& operator *= ( const T s ){
for (int j=0;j<i;j++) v[j]=v[j]*s;
return *this;
}
}
I define a new class Point2
template<class T> class Point2: public Point<T,2>
{
public:
Point2<T>()
{
v[0]=0;
v[1]=1;
}
Point2<T>(const T& a, const T& b)
{
v[0]=a;
v[1]=b;
}
T x() { return v[0] };
T y() { return v[1] };
};
and in main program I run:
Point2f a;
Point2f b;
Point2f c;
c=a+b;
I get this error:
error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'Point<T,i>' (or there is no acceptable conversion)
with
[
T=float,
i=2
]
could be 'Point2<T> &Point2<T>:perator =(const Point2<T>
&)'
with
[
T=float
]
while trying to match the argument list '(Point2f,
Point<T,i>)'
with
[
T=float,
i=2
]
Why Point2 don't call the operator= defined in Point<i,T>?
Point2 is a Point<i,T> because derive by Point<i,T>, I don't
understand...
Please explain me.