435,247 Members | 1,165 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

Convertion problem

 P: n/a I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc. I wish to write a function that I can call to "as required by comparison functions" to convert any value to a mm value so that when I'm comparing Distance objects the values will all be the same. The code I've come up with is below; however, it doesn't work correctly. When I try and compile it the compiler advises it must return a value; however, I simply want to replace the object that called to it with the new values, ie Distance a (2, cm) to Distance a (20, mm) etc. The constructor used in the class is as follows: Distance :: Distance (int n, char m) : nu(n), me(m) {} My function as follows: int Distance::to_mm() { if ( me == km) { nu = nu/1000000; me = mm; } if (me == m) { nu = nu/1000; me = mm; } if ( me == cm) { nu = nu/10; me = m; } } Thanks for any help Jul 22 '05 #1
6 Replies

 P: n/a "Chiller" <...@...> wrote... I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc. I wish to write a function that I can call to "as required by comparison functions" to convert any value to a mm value so that when I'm comparing Distance objects the values will all be the same. The code I've come up with is below; however, it doesn't work correctly. When I try and compile it the compiler advises it must return a value; however, I simply want to replace the object that called to it with the new values, ie Distance a (2, cm) to Distance a (20, mm) etc. The constructor used in the class is as follows: Distance :: Distance (int n, char m) : nu(n), me(m) {} My function as follows: int Distance::to_mm() You declared your function as returning an int. That means that inside the body of your function _all_ control paths have to lead to a statement of the kind return ; If you didn't intend to return anything from your function 'to_mm', you ought to declare it 'void': void Distance::to_mm() { if ( me == km) { nu = nu/1000000; me = mm; } if (me == m) I'd recommend else if (me == m) { nu = nu/1000; me = mm; } if ( me == cm) The same here. { nu = nu/10; me = m; } } Thanks for any help HTH Victor Jul 22 '05 #2

 P: n/a "Chiller" <...@...> wrote in message news:d1******************************@news.teranew s.com... using namespace std; Not in header files. Distance :: Distance (int n, char m) : nu(n), me(m) {} Distance :: Distance (void) : nu(0), me(2) {} enum {mm, cm, m, km}; enum Measure {mm=1, cm=10, m=1000, km=1000000}; Furthermore, if you do operator>, operator-, etc most often, it's best to store the result in millimeters. Distance :: Distance (int n=0, Measure m=mm) : d_n(n*m) {} Ultimately it's your call. BTW, member function measure should probably return a Measure, not a char. And the constructor should take a Measure. void Distance::to_mm() { if ( me == km) { nu = nu/1000000; me = mm; } Seems you want multiplication? // Overload of the "=" operator Distance & Distance::operator= (Distance const & right_operand) { nu = right_operand.nu; me = right_operand.me; return *this; } Compiler generated constructor is fine; no need to write your own. // Overload of the "==" operator bool Distance::operator == ( Distance const & rhs ) { to_mm(); return ( nu == rhs.nu && me == rhs.me ); } Really? I think 1000mm equals 1m, so Distance(1000,Distance::mm) == Distance(1,m) ought to return true. So the comparison function ought to take the measure into account. If you store all values in mm inside the class, then operator== is very easy to write. //Overload of the != operator bool Distance::operator != ( Distance const & rhs ) { to_mm(); return ( nu != rhs.nu && me != rhs.me ); } Should probably be implemented in terms of operator==. return !(lhs==rhs); Also, probably make operator== and the others as non-members for symmetry if there is a one-arg non-explicit constructor. // provide an overload of "<<" for easy display ostream& operator<< (ostream& out, Distance& d) { out << "(" << d.number() << ", "; switch (d.measure()) { case mm: out << "mm"; case cm: out << "cm"; break; case m: out << "m"; break; case km: out << "km"; break; } out << ")"; return out; } If you used enum {mm, cm, m, km}; then just out << "(" << d.number() << ", " << g_table[d.measure()] << ')'; where const char g_table = { "mm", "cm", "m", "km" }; What about operator>>, which is harder? Jul 22 '05 #4

 P: n/a "Chiller" <...@...> wrote in message news:cf******************************@news.teranew s.com... Distance :: Distance (int n, char m) : nu(n), me(m) {} My function as follows: int Distance::to_mm() { if ( me == km) { nu = nu/1000000; me = mm; } if (me == m) { nu = nu/1000; me = mm; } if ( me == cm) { nu = nu/10; me = m; } } Should all the multiplication signs be division? Also, the last else says me=m, but the others are me=mm. Jul 22 '05 #5

 P: n/a Distances are continuous. Storing them as integers is silly. And as Siemel Naran seems to have wanted to say, all those divisions should be multiplications. -- Regards, Buster. Jul 22 '05 #6

 P: n/a "Buster" wrote in message news:c5**********@newsg1.svr.pol.co.uk... Distances are continuous. Storing them as integers is silly. And as Siemel Naran seems to have wanted to say, all those divisions should be multiplications. Storing them as integers might make sense for some applications. Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion. 