Template et surcharge d'operateur affectation
Bonjour,
J'ai une classe Point template pour géré des coordonnées 2D :
Code:
1 2 3 4 5 6 7 8
| class Point
{
public:
T _x ;
T _y ;
Point( T x, T y ) : _x(x), _y(y) {}
}; |
Je peux donc l'utilisé avec des int ou des doubles... Mais j'aimerai faire une spécification pour faire une conversion:
Code:
1 2 3 4
| Point<int> p1(1,2);
Point<double> p2(3.2,4.7);
p2 = p1;
p1 = p2; |
Donc je me suis tourné vers une surcharge de l’opérateur d'affectation. Il me faudra spécialisé ensuite...
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class Point {
[...]
template<class U>
Point<T>& operator=(const Point<U>& p);
};
template<class T,class U>
Point<T>& Point<T>::operator=(const Point<U>& p)
{
_x = p._x ;
_y = p._y;
return (*this);
} |
Mais la mon compilo (Visual) ben il veux pas :
Citation:
1>error C2244: 'Point<T>::operator =' : unable to match function definition to an existing declaration
1> definition
1> 'Point<T> &Point<T>::operator =(const Point<U> &)'
1> existing declarations
1> 'Point<T> &Point<T>::operator =(const Point<U> &)'
Sa ce ressemble beaucoup quand même...
Voila, est ce que je tiens la bonne voie ? Merci d'avance