C++ compiler error *not solved!*, Another one that puzzles me :) |
![]() ![]() |
C++ compiler error *not solved!*, Another one that puzzles me :) |
Dec 25 2005, 05:06 PM
Post
#1
|
|
![]() Member ![]() ![]() Posts: 10 OS: Windows XP Pro |
Got another C++ problem. I keep getting this error message, but I don't know what I'm doing wrong? .\Interval.cpp(34) : error C2662: 'Interval::sign' : cannot convert 'this' pointer from 'const Interval' to 'Interval &' It's referring to this functions: CODE bool Interval::hasZero() const { if (sign(lo) != sign(hi)) <--- *this line* return true; else if ((lo == 0.0) || (hi == 0.0)) return true; else return false; } "lo" and "hi" are (double) attributes of Interval. The function sign() is not a member function, merely a support function to make the code easier to read. CODE int sign(double a) { if (a < 0.0) return -1; else return 1; } The above mentioned functions are both located in Interval.cpp This post has been edited by magnus80a: Dec 28 2005, 03:08 PM |
|
|
Dec 25 2005, 07:23 PM
Post
#2
|
|
|
New Member ![]() Posts: 5 From: South Korea OS: Win XP - Ubuntu 5 |
why did you declare your method as const ?
try without. |
|
|
Dec 25 2005, 08:35 PM
Post
#3
|
|
![]() Malware Expert Posts: 1,025 OS: Windows XP / Mandrake |
This is also going to give *huge* problems anyway because of the way C/C++ stores floats (doubles). Remember that floating-point arithmetic is only accurate up to a "precision" or "margin-of-error", so that even if you think you might have a number, say, equalling zero, it might actually internally be implemented as 0.00000000000001928357 (whatever, some garbage after a certain decimal place). So when you run your "sign" function you will get unexpected results. Perhaps there is a built-in function for retrieving floating-point sign that might be better? Moreover, these tests will pretty-much ALWAYS fail:
lo == 0.0 because lo will rarely ever be EXACTLY 0.0 ; rather, even when you THINK lo is zero it will really be 0.00000000000000001 , etc. So you might want to rethink this... |
|
|
Dec 25 2005, 08:50 PM
Post
#4
|
|
|
New Member ![]() Posts: 5 From: South Korea OS: Win XP - Ubuntu 5 |
you're right about the doubles and the problems to compare them.
but your sign function returns int, so, in this case, no problem, you'll get exactly 1 or -1 that's why the test sign(lo) != sign(hi) will be ok the often used solution to compare doubles is to compare their difference, for example if ((lo == 0.0) becomes if (fabs(l0) < EPSILON) with stg like #define EPSILON .0001 and the value given to EPSILON will depend of the type double or float. read the c++ norm and you'll find how many numbers are guaranteed to be exact and you'll know if you have to declare EPSILON as .0001 or .000001 or stg else. |
|
|
Dec 26 2005, 03:53 AM
Post
#5
|
|
![]() Member ![]() ![]() Posts: 10 OS: Windows XP Pro |
QUOTE(khayyam @ Dec 26 2005, 02:23 AM) [snapback]495217[/snapback] why did you declare your method as const ? try without. I thought that as long as a function doesn't change anything within the class it's consdiered a constant function? Regarding the double comparisons, you're right I've forgotten about floating point stuff. I'll try and se if I can compute my machine eps. If there is one I might as well declare that as machine eps. *edit* Tried removing the constant part, but it still complains. It looks like it doesn't like non constant function being used by constant functions either. The error message is the same as before. CODE Interval Interval::operator *(const Interval& obj) const { if ((hi < 0.0) && (obj.lo > 0.0)) return Interval(lo*obj.hi, hi*obj.lo); else if (hasZero() && (obj.lo > 0.0)) *<--- this line* return Interval(lo*obj.hi, hi*obj.hi); else if ((lo > 0.0) && (obj.lo > 0.0)) return Interval(lo*obj.lo, hi*obj.hi); else if ((hi < 0.0) && (obj.hasZero())) *<--- this line* return Interval(lo*obj.hi, lo*obj.lo); else if ((lo > 0.0) && (obj.hasZero())) *<--- this line* return Interval(hi*obj.lo, hi*obj.hi); else if ((hi < 0.0) && (obj.hi < 0.0)) return Interval(hi*obj.hi, lo*obj.lo); else if ((hasZero()) && (obj.hi < 0.0)) *<--- this line* return Interval(hi*obj.lo, lo*obj.lo); else if ((lo > 0.0) && (obj.hi < 0.0)) return Interval(hi*obj.lo, lo*obj.hi); else { return Interval(min(min(lo*obj.lo, lo*obj.hi), min(hi*obj.lo, hi*obj.hi)), *<--- this line* max(max(lo*obj.lo, lo*obj.hi), max(hi*obj.lo, hi*obj.hi))); *<--- this line* } } CODE double min(double a, double b) {
if (a <= b) return a; else return b; } double max(double a, double b) { if (a <= b) return b; else return a; } This post has been edited by magnus80a: Dec 26 2005, 04:19 AM |
|
|
![]() ![]() |
Similar Topics
| Topic Title | Replies / Views | Topic Information | |||||
|---|---|---|---|---|---|---|---|
![]() |
5 / 2,562 | 21st May 2005 - 11:16 PM Twell started - last by Twell |
|||||
![]() |
6 / 1,763 | 6th December 2005 - 01:59 PM magnus80a started - last by magnus80a |
|||||
![]() |
1 / 183 | 24th June 2007 - 01:50 AM thefrancislee started - last by Crustyoldbloke |
|||||
![]() |
0 / 42 | 3rd January 2009 - 10:40 PM lrob6 started - last by lrob6 |
|||||
|
Time is now: 8th January 2009 - 03:44 AM |
| Advertisements do not imply our endorsement of that product or service. The forum is run by volunteers who donate their time and expertise. We make every attempt to ensure that the help and advice posted is accurate and will not cause harm to your computer. However, we do not guarantee that they are accurate and they are to be used at your own risk. |