LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Compilation problem overloading operator () to template class constructor. (https://www.linuxquestions.org/questions/programming-9/compilation-problem-overloading-operator-to-template-class-constructor-436798/)

ianix 04-19-2006 12:51 PM

Compilation problem overloading operator () to template class constructor.
 
I have the following class:

template<class T>
class Color {

public:
T red, green, blue;

Color() {

red =green = blue = (T)0;

};

Color(T r, T g, T b){
red = r;
green = g;
blue = b;
};


Color<T> toGray() const {

T i = intensity();
return Color<T>(i,i,i);

};
friend Color<T> operator*(float v, Color<T> c){

return Color<T>((T)(c.red*v), (T)(c.green*v),(T)(c.blue*v));

};


friend Color<T> operator*(Color<T> c, float v){

return v*c;

};

friend Color<T> operator*(double v, Color<T> c){

return Color<T>((T)(c.red*v), (T)(c.green*v),(T)(c.blue*v));

};

friend Color<T> operator*(Color<T> c, double v){

return v*c;

};

// Type conversion operators
template<T>
Color<T> :: operator Color<float>() {

return Color<float>((float)red,(float)green,(float)blue);

};

template<T>
Color<T>:: operator Color<unsigned char>(){

return Color<unsigned char>((unsigned char)red,(unsigned char)green,
(unsigned char)blue);

};
template<T>
Color<T>:: operator Color<double>(){

return Color<double>((double)red,(double)green,(double)blue);

};
template<T>
Color<T>:: operator Color<short int>(){

return Color<short int>((int)red,(int)green,(int)blue);

};
};

// global iostream operators
template<class T>
ostream& operator << (ostream & os, Color<T> c) {

os << "Color(" << (double)c.red << "," << (double)c.green <<
"," << (double)c.blue << ")";
return os;
} ;


When I use the class above in the code below:

Color<ImType> RetinalImage::integrate(Point cRfield, float rRfield) const {

Color<double> accColor(0,0,0); // color accumulator
double accIntegr=0;

newColor=(sourceImg->getColor(Point(n,m))).logColor();
double integr = rff->getValue(currentR, rRfield);
if((m>=0) && (m<sourceImg->ySize) && (n>=0) && (n<sourceImg->xSize))
accIntegr+=integr;
//---------Error in next line-----------------
accColor = (Color<double>)accColor + (Color<double>)((Color<double>)newColor * integr);
//--------Error in the line above---------------
accColor = accColor/(accIntegr?accIntegr:1.0);
//---------Error in next line-----------------
return (Color<ImType>) accColor;
//--------Error in the line above---------------
}


I get the following errors:

Image.cc: In member function 'Color<float> RetinalImage::integrate(Point, float) const':
Image.cc:1318: error: no matching function for call to 'Color<double>::Color(Color<float>&)'
Color.h:53: note: candidates are: Color<T>::Color(T, T, T) [with T = double]
Color.h:47: note: Color<T>::Color() [with T = double]
Color.h:31: note: Color<double>::Color(const Color<double>&)
Image.cc:1330: error: no matching function for call to 'Color<float>::Color(Color<double>&)'
Color.h:53: note: candidates are: Color<T>::Color(T, T, T) [with T = float]
Color.h:47: note: Color<T>::Color() [with T = float]
Color.h:31: note: Color<float>::Color(const Color<float>&)


Anyone could help me?

addy86 04-19-2006 01:16 PM

Please use CODE-tags the next time; it was a plight to read your code.

Your problem is that you're lacking a "conversion constructor" for Color from float to double (although float can be implicitely casted to double, this is not possible for Color<float> to Color<double>).


All times are GMT -5. The time now is 10:22 AM.