435,247 Members | 1,165 Online
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