Topic: const problem using a template
Author: tpv01@uow.edu.au (Timothy Philip Vernum)
Date: 1998/03/18 Raw View
"Peter Mancini" <peter_mancini@onesource.com> writes:
[snip]
>C:\Program Files\DevStudio\VC\INCLUDE\xutility(45 + 47) : error C2678:
>binary '<' : no operator defined which takes a left-hand operand of type
>'const class CNewsItem' (or there is no acceptable conversion)
[snip]
>CNewsItem is loaded into a Vector in CNewsStore. I have an operator<()
>defined. I do have 1 const function but I don't see how it relates to
>operator<().
Well the problem is (as you note) that it wants an operator<()
Now, it wants to be able to do is
operator<( const class CNewsItem& lhs, const class CNewsItem& rhs ) ;
(where the pass-by-reference is optional)
Presumably the template function you are calling is taking const arguments,
and thus needs to be able to pass them to the operator (hence the reference
to "const class CNewsItem")
Now, your member operator has an implicit first argument which is actually
a pointer to a class CNewsItem ( ie 'this' ), but unless you declared the
member operator as 'const', it's not a a const class CNewsItem.
Just make the comparison operator const.
ie
class CNewsItem
{
//...
bool operator < ( const CNewsItem& rhs ) const ;
//...
} ;
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: agriffiths@ma.ccngroup.com (Alan Griffiths)
Date: 1998/03/18 Raw View
In article: <6em7bt$221@fridge.shore.net> "Peter Mancini" <peter_mancini@onesource.com> writes:
>
. . .
> C:\Program Files\DevStudio\VC\INCLUDE\xutility(45 + 47) : error C2678:
> binary '<' : no operator defined which takes a left-hand operand of type
> 'const class CNewsItem' (or there is no acceptable conversion)
. . .
> CNewsItem is loaded into a Vector in CNewsStore. I have an operator<()
> defined.
But do you have bool CNewsItem::operator<()(const CNewsItem&) const
defined? (Note the final const.)
__
Alan Griffiths | Senior Systems Consultant, Experian (Formerly CCN)
alan.griffiths@experian.com (agriffiths@ma.ccngroup.com, Tel. +44 115 934 4517)
(ACCU C++ SIG organiser | <http://www.accu.org/> alan@octopull.demon.co.uk)
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Hyman Rosen <uunet!jyacc!hymie@ncar.UCAR.EDU>
Date: 1998/03/18 Raw View
"Peter Mancini" <peter_mancini@onesource.com> writes:
> CNewsItem is loaded into a Vector in CNewsStore. I have an operator<()
> defined. I do have 1 const function but I don't see how it relates to
> operator<(). I'm a bit stuck and was wondering if anyone could give me some
> general thoughts on where I might look for the problem. I can provide more
> detailed examples of code.
You neglected the single crucial piece of information, which is the
definition of your operator<. To work correctly, it should look like
this, as a member function:
class CNewsItem
{
...
public:
bool operator <(const CNewsItem &other) const;
}
or like this, as a non-member:
bool operator <(const CNewsItem &left, const CNewsItem &right);
If this doesn't work for you, it may be that your compiler is having
trouble with Koenig lookup. (I.E., lexicographical_compare is in a
different namespace than CNewsItem, and it may not be looking in the
namespace of CNewsItem for the declaration of operator <.)
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Peter Mancini" <peter_mancini@onesource.com>
Date: 1998/03/18 Raw View
I am converting a VC++ 4.2 project to VC++ 5.0. 5.0 uses a version of
standard C++ that implements the STL more correctly than 4.2 did. Most of
the changes are things like adding "using namespace std" and changing STL
constructor calls to not use the allocator<class> that was required
previously.
All is well and good, I got rid of every error excepts 1 and it is killing
me. The error reads:
C:\Program Files\DevStudio\VC\INCLUDE\xutility(45 + 47) : error C2678:
binary '<' : no operator defined which takes a left-hand operand of type
'const class CNewsItem' (or there is no acceptable conversion)
And comes from the following code in xutility.cpp
// TEMPLATE FUNCTION lexicographical_compare
template<class _II1, class _II2> inline
bool lexicographical_compare(_II1 _F1, _II1 _L1,
_II2 _F2, _II2 _L2)
{for (; _F1 != _L1 && _F2 != _L2; ++_F1, ++_F2)
if (*_F1 < *_F2) //Error here
return (true);
else if (*_F2 < *_F1) //and Here
return (false);
return (_F1 == _L1 && _F2 != _L2); }
CNewsItem is loaded into a Vector in CNewsStore. I have an operator<()
defined. I do have 1 const function but I don't see how it relates to
operator<(). I'm a bit stuck and was wondering if anyone could give me some
general thoughts on where I might look for the problem. I can provide more
detailed examples of code.
Thanks,
Peter Mancini
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]