[Problème] Alias de spécialisation de template
Bonjour,
Je suis en train de créer une structure template permettant de stocker une couleur codée sur 3 canaux (ex: RGB, XYZ, HSV, Lab ...)
Le problème c'est que selon l'espace utilisé, les composantes n'auront pas le même type (ex: En RGB les composantes sont en unsigned int alors qu'en Lab ce sont des double)
Voici le template TColor qui définit les opérations de base sur des couleurs :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
template <class T>
class TColor
{
public:
// Les 3 composantes couleurs
T c1;
T c2;
T c3;
inline TColor();
inline TColor(T c1, T c2, T c3);
inline ~TColor();
inline TColor operator = (const TColor<T> &value);
inline TColor operator + (const T &value);
inline TColor operator - (const T &value);
inline TColor operator / (const T &value);
inline TColor operator * (const T &value);
inline bool operator== (T value) const;
inline bool operator!= (T value) const;
inline bool operator == (TColor<T> &value) const;
inline bool operator != (TColor<T> &value) const;
inline TColor operator+ (const TColor<T> &value);
inline TColor operator- (const TColor<T> &value);
inline TColor operator/ (const TColor<T> &value);
inline TColor operator* (const TColor<T> &value);
};
template <class T>
TColor<T>::TColor()
{
this->c1 = 0;
this->c2 = 0;
this->c3 = 0;
};
template <class T>
TColor<T>::~TColor()
{
};
template <class T>
TColor<T>::TColor(T c1, T c2, T c3)
{
this->c1 = c1;
this->c2 = c2;
this->c3 = c3;
};
template <class T>
TColor<T> TColor<T>::operator = (const TColor<T> &value)
{
this->c1 = value.c1;
this->c2 = value.c2;
this->c3 = value.c3;
}
template <class T>
TColor<T> TColor<T>::operator + (const T &value)
{
TColor<T> tmp = (*this);
tmp.c1 += value;
tmp.c2 += value;
tmp.c3 += value;
return tmp;
};
template <class T>
TColor<T> TColor<T>::operator - (const T &value)
{
TColor<T> tmp = (*this);
tmp.c1 -= value;
tmp.c2 -= value;
tmp.c3 -= value;
return tmp;
};
template <class T>
TColor<T> TColor<T>::operator / (const T &value)
{
TColor<T> tmp = (*this);
tmp.c1 /= value;
tmp.c2 /= value;
tmp.c3 /= value;
return tmp;
};
template <class T>
TColor<T> TColor<T>::operator * (const T &value)
{
TColor<T> tmp = (*this);
tmp.c1 *= value;
tmp.c2 *= value;
tmp.c3 *= value;
return tmp;
};
template <class T>
bool TColor<T>::operator== (T value) const
{
return (this->c1==value) && (this->c2==value) && (this->c3==value);
};
template <class T>
bool TColor<T>::operator!= (T value) const
{
return (this->c1!=value) || (this->c2!=value) || (this->c3!=value);
}
template <class T>
bool TColor<T>::operator == (TColor<T> &value) const
{
return (this->c1==value.c1) && (this->c2==value.c2) && (this->c3==value.c3);
};
template <class T>
bool TColor<T>::operator != (TColor<T> &value) const
{
return (this->c1!=value.c1) || (this->c2!=value.c2) || (this->c3!=value.c3);
};
template <class T>
TColor<T> TColor<T>::operator + (const TColor<T> &value)
{
TColor<T> tmp = (*this);
tmp.c1 += value.c1;
tmp.c2 += value.c2;
tmp.c3 += value.c3;
return tmp;
};
template <class T>
TColor<T> TColor<T>::operator - (const TColor<T> &value)
{
TColor<T> tmp = (*this);
tmp.c1 -= value.c1;
tmp.c2 -= value.c2;
tmp.c3 -= value.c3;
return tmp;
};
template <class T>
TColor<T> TColor<T>::operator / (const TColor<T> &value)
{
TColor<T> tmp = (*this);
tmp.c1 /= value.c1;
tmp.c2 /= value.c2;
tmp.c3 /= value.c3;
return tmp;
};
template <class T>
TColor<T> TColor<T>::operator * (const TColor<T> &value)
{
TColor<T> tmp = (*this);
tmp.c1 *= value.c1;
tmp.c2 *= value.c2;
tmp.c3 *= value.c3;
return tmp;
}; |
J'aimerai maintenant pouvoir, pour chaque espace de couleur, spécialiser et renommer la classe ainsi que rajouter des méthodes. Je m'explique...
Je voudrais :
- une classe TLAB qui spécialise TColor<double> et qui rajoute getL(), getA() et getB() des accesseurs sur c1, c2 et c3
- une classe TRGB qui spécialise TColor<unsigned int> et qui rajoute getR(), getG() et getB() des accesseurs sur c1, c2 et c3
- etc pour chaque espace de couleur
Sans avoir bien sur à redéclarer toute la classe à chaque fois que je rajoute un espace de couleur.
j'ai essayé ça :
Code:
1 2 3 4 5 6 7 8
|
class TLAB : public TColor<double>
{
public:
double getL();
double getA();
double getB();
}; |
Mais l'héritage n'a pas l'air de fonctionner puisque quand j'essaye de compiler ça dans le main :
Code:
1 2
|
TLAB col1(3., 4., 5.); |
je reçois une erreur de compil :
error: no matching function for call to `TLAB::TLAB(double, double, double)'
candidates are:
TLAB::TLAB()
TLAB::TLAB(const TLAB&)
...
J'ai aussi essayé ceci qui fonctionne :
Code:
1 2
|
typedef TColor<double> TLAB; |
mais dans ce cas, je ne peux pas rajouter les méthodes getL(), getA() et get(B)...
Avez-vous une idée pour résoudre ce casse-tête?
Merci d'avance,