Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Binary Predicate for Templated Class

Binary Predicate for Templated Class

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studiographicsalgorithmshelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mjackson11
    wrote on last edited by
    #1

    I created a binary predicate // binary predicate for searching by exp date bool exp_LT(curvePoint lhs, curvePoint rhs) { return (lhs.getExp() < rhs.getExp()); } so I could search a vector by _expDate rather than _mDate on the curvePoint class: template class curvePoint : public subscriber { friend fwdCurve; friend bktCurve; public: void setValue(_Ty p) { _mValue = p; } _Ty getValue() { return _mValue; } void setDate(Date d) { _mDate = d; } inline void setDate(long d) { _mDate = d; } Date getDate() { return _mDate; } void setExp(Date d) { _expDate = d; } void setExp(long d) { _expDate = d; } Date getExp() { return _expDate; } void update() {} // allow self comparison bool operator<(const curvePoint<_Ty>& rhs) const { return _mDate < rhs._mDate; } bool operator>(const curvePoint<_Ty>& rhs) const { return _mDate > rhs._mDate; } bool operator==(const curvePoint<_Ty>& rhs) const { return _mDate == rhs._mDate; } bool operator!=(const curvePoint<_Ty>& rhs) const { return _mDate != rhs._mDate; } protected: _Ty _mValue; Date _mDate; Date _expDate; }; When I call curvePoint c; c.setExp(myDate("01/01/2009")); i = lower_bound(vector.begin(), vector.end(), &c, exp_LT); <---- Crashes here The compiler throws an error (C2664). The message is 1>c:\program files\microsoft visual studio 8\vc\include\xutility(312) : error C2664: 'bool (RAIV::curvePoint<_Ty>,RAIV::curvePoint<_Ty>)' : cannot convert parameter 2 from 'RAIV::curvePoint<_Ty> ' to 'RAIV::curvePoint<_Ty>' 1> with 1> [ 1> _Ty=double 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous I'm guessing the compiler cannot resolve something about the templated curvePoint parameter but am at a loss as to how to proceed.

    L D M S 4 Replies Last reply
    0
    • M mjackson11

      I created a binary predicate // binary predicate for searching by exp date bool exp_LT(curvePoint lhs, curvePoint rhs) { return (lhs.getExp() < rhs.getExp()); } so I could search a vector by _expDate rather than _mDate on the curvePoint class: template class curvePoint : public subscriber { friend fwdCurve; friend bktCurve; public: void setValue(_Ty p) { _mValue = p; } _Ty getValue() { return _mValue; } void setDate(Date d) { _mDate = d; } inline void setDate(long d) { _mDate = d; } Date getDate() { return _mDate; } void setExp(Date d) { _expDate = d; } void setExp(long d) { _expDate = d; } Date getExp() { return _expDate; } void update() {} // allow self comparison bool operator<(const curvePoint<_Ty>& rhs) const { return _mDate < rhs._mDate; } bool operator>(const curvePoint<_Ty>& rhs) const { return _mDate > rhs._mDate; } bool operator==(const curvePoint<_Ty>& rhs) const { return _mDate == rhs._mDate; } bool operator!=(const curvePoint<_Ty>& rhs) const { return _mDate != rhs._mDate; } protected: _Ty _mValue; Date _mDate; Date _expDate; }; When I call curvePoint c; c.setExp(myDate("01/01/2009")); i = lower_bound(vector.begin(), vector.end(), &c, exp_LT); <---- Crashes here The compiler throws an error (C2664). The message is 1>c:\program files\microsoft visual studio 8\vc\include\xutility(312) : error C2664: 'bool (RAIV::curvePoint<_Ty>,RAIV::curvePoint<_Ty>)' : cannot convert parameter 2 from 'RAIV::curvePoint<_Ty> ' to 'RAIV::curvePoint<_Ty>' 1> with 1> [ 1> _Ty=double 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous I'm guessing the compiler cannot resolve something about the templated curvePoint parameter but am at a loss as to how to proceed.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      mjackson11 wrote:

      bool exp_LT(curvePoint lhs, curvePoint rhs) { return (lhs.getExp() < rhs.getExp()); }

      Don't you want those to be references?

      led mike

      1 Reply Last reply
      0
      • M mjackson11

        I created a binary predicate // binary predicate for searching by exp date bool exp_LT(curvePoint lhs, curvePoint rhs) { return (lhs.getExp() < rhs.getExp()); } so I could search a vector by _expDate rather than _mDate on the curvePoint class: template class curvePoint : public subscriber { friend fwdCurve; friend bktCurve; public: void setValue(_Ty p) { _mValue = p; } _Ty getValue() { return _mValue; } void setDate(Date d) { _mDate = d; } inline void setDate(long d) { _mDate = d; } Date getDate() { return _mDate; } void setExp(Date d) { _expDate = d; } void setExp(long d) { _expDate = d; } Date getExp() { return _expDate; } void update() {} // allow self comparison bool operator<(const curvePoint<_Ty>& rhs) const { return _mDate < rhs._mDate; } bool operator>(const curvePoint<_Ty>& rhs) const { return _mDate > rhs._mDate; } bool operator==(const curvePoint<_Ty>& rhs) const { return _mDate == rhs._mDate; } bool operator!=(const curvePoint<_Ty>& rhs) const { return _mDate != rhs._mDate; } protected: _Ty _mValue; Date _mDate; Date _expDate; }; When I call curvePoint c; c.setExp(myDate("01/01/2009")); i = lower_bound(vector.begin(), vector.end(), &c, exp_LT); <---- Crashes here The compiler throws an error (C2664). The message is 1>c:\program files\microsoft visual studio 8\vc\include\xutility(312) : error C2664: 'bool (RAIV::curvePoint<_Ty>,RAIV::curvePoint<_Ty>)' : cannot convert parameter 2 from 'RAIV::curvePoint<_Ty> ' to 'RAIV::curvePoint<_Ty>' 1> with 1> [ 1> _Ty=double 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous I'm guessing the compiler cannot resolve something about the templated curvePoint parameter but am at a loss as to how to proceed.

        D Offline
        D Offline
        Dan 0
        wrote on last edited by
        #3

        You can try

        template< typename T >
        struct curve_lt : public binary_function <T, T, bool>
        {
        bool operator()( const T &rhs, const T &lhs )
        {
        return rhs.getDate() < lhs.getData();
        }
        };
        ......
        i = lower_bound(vector.begin(), vector.end(), &c, curve_lt());

        1 Reply Last reply
        0
        • M mjackson11

          I created a binary predicate // binary predicate for searching by exp date bool exp_LT(curvePoint lhs, curvePoint rhs) { return (lhs.getExp() < rhs.getExp()); } so I could search a vector by _expDate rather than _mDate on the curvePoint class: template class curvePoint : public subscriber { friend fwdCurve; friend bktCurve; public: void setValue(_Ty p) { _mValue = p; } _Ty getValue() { return _mValue; } void setDate(Date d) { _mDate = d; } inline void setDate(long d) { _mDate = d; } Date getDate() { return _mDate; } void setExp(Date d) { _expDate = d; } void setExp(long d) { _expDate = d; } Date getExp() { return _expDate; } void update() {} // allow self comparison bool operator<(const curvePoint<_Ty>& rhs) const { return _mDate < rhs._mDate; } bool operator>(const curvePoint<_Ty>& rhs) const { return _mDate > rhs._mDate; } bool operator==(const curvePoint<_Ty>& rhs) const { return _mDate == rhs._mDate; } bool operator!=(const curvePoint<_Ty>& rhs) const { return _mDate != rhs._mDate; } protected: _Ty _mValue; Date _mDate; Date _expDate; }; When I call curvePoint c; c.setExp(myDate("01/01/2009")); i = lower_bound(vector.begin(), vector.end(), &c, exp_LT); <---- Crashes here The compiler throws an error (C2664). The message is 1>c:\program files\microsoft visual studio 8\vc\include\xutility(312) : error C2664: 'bool (RAIV::curvePoint<_Ty>,RAIV::curvePoint<_Ty>)' : cannot convert parameter 2 from 'RAIV::curvePoint<_Ty> ' to 'RAIV::curvePoint<_Ty>' 1> with 1> [ 1> _Ty=double 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous I'm guessing the compiler cannot resolve something about the templated curvePoint parameter but am at a loss as to how to proceed.

          M Offline
          M Offline
          mjackson11
          wrote on last edited by
          #4

          It turned out that I called the routine correctly 5 times, on the sixth call, I had the wrong kind of parameter there, (an iterator as opposed to the actual class). Somehow that caused an error in all six places the function was called.

          1 Reply Last reply
          0
          • M mjackson11

            I created a binary predicate // binary predicate for searching by exp date bool exp_LT(curvePoint lhs, curvePoint rhs) { return (lhs.getExp() < rhs.getExp()); } so I could search a vector by _expDate rather than _mDate on the curvePoint class: template class curvePoint : public subscriber { friend fwdCurve; friend bktCurve; public: void setValue(_Ty p) { _mValue = p; } _Ty getValue() { return _mValue; } void setDate(Date d) { _mDate = d; } inline void setDate(long d) { _mDate = d; } Date getDate() { return _mDate; } void setExp(Date d) { _expDate = d; } void setExp(long d) { _expDate = d; } Date getExp() { return _expDate; } void update() {} // allow self comparison bool operator<(const curvePoint<_Ty>& rhs) const { return _mDate < rhs._mDate; } bool operator>(const curvePoint<_Ty>& rhs) const { return _mDate > rhs._mDate; } bool operator==(const curvePoint<_Ty>& rhs) const { return _mDate == rhs._mDate; } bool operator!=(const curvePoint<_Ty>& rhs) const { return _mDate != rhs._mDate; } protected: _Ty _mValue; Date _mDate; Date _expDate; }; When I call curvePoint c; c.setExp(myDate("01/01/2009")); i = lower_bound(vector.begin(), vector.end(), &c, exp_LT); <---- Crashes here The compiler throws an error (C2664). The message is 1>c:\program files\microsoft visual studio 8\vc\include\xutility(312) : error C2664: 'bool (RAIV::curvePoint<_Ty>,RAIV::curvePoint<_Ty>)' : cannot convert parameter 2 from 'RAIV::curvePoint<_Ty> ' to 'RAIV::curvePoint<_Ty>' 1> with 1> [ 1> _Ty=double 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous I'm guessing the compiler cannot resolve something about the templated curvePoint parameter but am at a loss as to how to proceed.

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            On a side note, a few changes will makes things better. First change this function by adding the underlined bits:

            bool exp_LT(const curvePoint<double> &lhs, const curvePoint<double> &rhs) { return (lhs.getExp() < rhs.getExp()); }

            Now change the class as follows (again, by adding the underlined bits):

            public:
            void setValue(_Ty p) { _mValue = p; }
            _Ty getValue() const { return _mValue; }
            void setDate(Date d) { _mDate = d; }
            inline void setDate(long d) { _mDate = d; }
            Date getDate() const { return _mDate; }
            void setExp(Date d) { _expDate = d; }
            void setExp(long d) { _expDate = d; }
            Date getExp() const { return _expDate; }

            Steve

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups