|
From: Igor Tandetnik on 9 May 2008 12:32 3DCoderGuy <nobody(a)nospam.com> wrote: > Igor Tandetnik wrote: >> Apparently, you want to specialize a member function of class >> template. Such a specialization must go outside the class >> definition, and looks like this: >> >> template<> >> bool const XYZPoint<float>::operator==(const XYZPoint<float>& >> xyzTest) const; >> > > Thanks Igor, > VC 2003 doesn't like the syntax you have suggested. Show the exact code you are compiling, and quote the exact error message. My mind reading this days is not what it used to be. -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Giovanni Dicanio on 9 May 2008 12:37 "Igor Tandetnik" <itandetnik(a)mvps.org> ha scritto nel messaggio news:%235mz%23IfsIHA.1316(a)TK2MSFTNGP06.phx.gbl... >>> template<> >>> bool const XYZPoint<float>::operator==(const XYZPoint<float>& >>> xyzTest) const; >>> >> >> Thanks Igor, >> VC 2003 doesn't like the syntax you have suggested. I think that the problem is "bool const" in "bool const XYZPoint..." But it is an OP's original code problem... Moreover, I would define binary operators like operator== as global (not class member). bool operator==( const XYZPoint<float> & , const XYZPoint<float> & ) Giovanni
From: 3DCoderGuy on 9 May 2008 12:40 3DCoderGuy wrote: > I'm trying to specialize the operator== for my template, here is my code > > #define DOUBLE_EPSILON (1e-6) > #define FLOAT_EPSILON (1e-4f) > > bool const operator==(const XYZPoint<T> &xyzTest) const > { > return ( > (x == xyzTest.x) && > (y == xyzTest.y) && > (z == xyzTest.z) > ); > }; > > template<typename T> > bool const operator==(const XYZPoint<float> &xyzTest) const > { > return ( > (x - xyzTest.x < FLOAT_EPSILON) && (x - xyzTest.x > -FLOAT_EPSILON) && > (y - xyzTest.y < FLOAT_EPSILON) && (y - xyzTest.y > -FLOAT_EPSILON) && > (z - xyzTest.z < FLOAT_EPSILON) && (z - xyzTest.z > -FLOAT_EPSILON) > ); > } > > template<typename T> > bool const operator==(const XYZPoint<double> &xyzTest) const > { > return ( > (x - xyzTest.x < DOUBLE_EPSILON) && (x - xyzTest.x > -DOUBLE_EPSILON) && > (y - xyzTest.y < DOUBLE_EPSILON) && (y - xyzTest.y > -DOUBLE_EPSILON) && > (z - xyzTest.z < DOUBLE_EPSILON) && (z - xyzTest.z > -DOUBLE_EPSILON) > ); > } > > But when I do this > XYZPoint<double> pnt1(1.0,0.0,1.0); > XYZPoint<double> pnt2(1.0,0.0,2.0); > > if (pnt1 == pnt2) > { > ... > } > > the specialization for <double> is never called. > > Can this be done, and what would be the correct syntax? > > Thanks > Mark > > I may have stumbled onto the answer. This syntax is working. template<typename T> class XYZPoint { template<typename T> bool const operator==(const XYZPoint<T> &xyzTest) const bool const operator==(const XYZPoint<float> &xyzTest) const bool const operator==(const XYZPoint<double> &xyzTest) const }; I'm new to templates so I don't understand why (and would like too if some one can explain). But this works. Mark
From: Giovanni Dicanio on 9 May 2008 12:41 "3DCoderGuy" <nobody(a)nospam.com> ha scritto nel messaggio news:urvIQHfsIHA.4876(a)TK2MSFTNGP02.phx.gbl... > I understand some of the issues with floating point number comparisons > (I'm not a mathematician). But I thought that was the whole point of > doing this type of comparison? > If my FLOAT_EPSILON is 1e-4f then am I not helping the comparison to > ignore the floating point issue so that a==b && b==c && a==c. > I'm not trying to land a ship on the moon, but if there is a flaw with my > design please let me know. IMHO, the "fuzzy" comparison for floating point is correct. (And I think that Igor's post was not against that - he was just making a fine good point about the non-transitiveness of this implementation of operator==.) An alternative to your code might be (assuming that your XYZPoint class has a Length() method and overloads operator- for vector difference): bool operator==( const XYZPoint<float> & v1, const XYZPoint<float> & v2 ) { return ( v1 - v2 ).Length() < FLOAT_EPSILON; } Giovanni
From: 3DCoderGuy on 9 May 2008 12:57 Igor Tandetnik wrote: > 3DCoderGuy <nobody(a)nospam.com> wrote: >> Igor Tandetnik wrote: >>> Apparently, you want to specialize a member function of class >>> template. Such a specialization must go outside the class >>> definition, and looks like this: >>> >>> template<> >>> bool const XYZPoint<float>::operator==(const XYZPoint<float>& >>> xyzTest) const; >>> >> Thanks Igor, >> VC 2003 doesn't like the syntax you have suggested. > > Show the exact code you are compiling, and quote the exact error > message. My mind reading this days is not what it used to be. Well strike me dumb. I did a simple class template with your suggestion and it worked. I must have mistyped something the first time. I'm going to relook at using a global compare as Giovanni suggests. Thanks Mark
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Serial Communication Problem Next: Improving MFC Window UI design |