LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2006, 12:51 PM   #1
ianix
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Rep: Reputation: 0
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?
 
Old 04-19-2006, 01:16 PM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
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>).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading a template allomeen Programming 1 01-26-2006 06:59 PM
Homework Help: Copy Constructor on a template class. MicahCarrick Programming 2 01-22-2006 10:43 PM
overloading an assignment operator to change one class to one that derives it byteframe Programming 1 12-19-2005 04:52 PM
problem compiling with overloading method 'operator<<' freeindy Programming 4 04-29-2005 08:58 AM
Operator Overloading problem ashwinipahuja Programming 1 06-23-2004 05:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration