Operator problem with std::pair

L

Leo Meyer

Hello,

if I don't describe this problem properly, please ask me, I'm not an expert on the STL. This thing has me quite baffled.
I'm porting a program to Pocket PC 2002 using STLPort using MS EVC3 and I keep running into this problem:

....\STLport-5.2.1\stlport\stl/_algobase.c(198) : error C2678: binary '==' : no operator defined
which takes a left-hand operand of type 'struct stlp_std::pair<class CIMGFile,bool>' (or there is no acceptable conversion)

The problem occurs in an implementation of find:
template <class _InputIter, class _Tp>
_STLP_INLINE_LOOP _InputIter __find(_InputIter __first, _InputIter __last,
const _Tp& __val,
const input_iterator_tag &) {
while (__first != __last && !(*__first == __val)) ++__first;
^ on this line the error is reported
return __first;
}

As far as I can tell, the pair is being constructed with this macro call:
std::make_pair(CIMGFile(), true)

CIMGFile is a proper class that seems to compile ok.

Previously, in a header file, the programmer defined the == operator using:
inline bool operator == (std::pair<CIMGFile, bool> & p, int id )
{
return p.first.GetID() == id;
}

So STLPort keeps telling me that there is no == operator while there clearly should be. Note that it doesn't work either if I change
std::pair to stlp_std::pair or make it const in the operator definition. I have also tried "int & id" instead with no effect.
Is there something wrong about the type signature that I am missing? How can I get this operator definition to work?

Kind regards, Leo
 
J

Juha Nieminen

Leo said:
inline bool operator == (std::pair<CIMGFile, bool> & p, int id )

I can't tell (without actually trying) if it's the underlying problem,
but comparators should always take const references, not non-const
references, like you do above.
 
L

Leo Meyer

Juha said:
I can't tell (without actually trying) if it's the underlying
problem, but comparators should always take const references, not
non-const references, like you do above.

Hi,
I have tried making both parameters const, such as:
inline bool operator == (const std::pair<CIMGFile, bool> & p, const int id )
but it doesn't work either.

Thanks and regards, Leo
 
A

Alf P. Steinbach

* Leo Meyer:
Hello,

if I don't describe this problem properly, please ask me, I'm not an expert on the STL. This thing has me quite baffled.
I'm porting a program to Pocket PC 2002 using STLPort using MS EVC3 and I keep running into this problem:

...\STLport-5.2.1\stlport\stl/_algobase.c(198) : error C2678: binary '==' : no operator defined
which takes a left-hand operand of type 'struct stlp_std::pair<class CIMGFile,bool>' (or there is no acceptable conversion)

The problem occurs in an implementation of find:
template <class _InputIter, class _Tp>
_STLP_INLINE_LOOP _InputIter __find(_InputIter __first, _InputIter __last,
const _Tp& __val,
const input_iterator_tag &) {
while (__first != __last && !(*__first == __val)) ++__first;
^ on this line the error is reported
return __first;
}

As far as I can tell, the pair is being constructed with this macro call:
std::make_pair(CIMGFile(), true)

CIMGFile is a proper class that seems to compile ok.

Previously, in a header file, the programmer defined the == operator using:
inline bool operator == (std::pair<CIMGFile, bool> & p, int id )
{
return p.first.GetID() == id;
}

So STLPort keeps telling me that there is no == operator while there clearly should be. Note that it doesn't work either if I change
std::pair to stlp_std::pair or make it const in the operator definition. I have also tried "int & id" instead with no effect.
Is there something wrong about the type signature that I am missing? How can I get this operator definition to work?

Try to reproduce the problem in a minimal program.

Cheers & hth.,

- Alf
 
L

Leo Meyer

Alf said:
[operator problem]
Try to reproduce the problem in a minimal program.

Cheers & hth.,

- Alf

I have replaced the offending find macro call with a simple loop. It compiles now.
Too many other problems for building a designated test case ;-)
Regards, Leo
 
J

James Kanze

if I don't describe this problem properly, please ask me, I'm
not an expert on the STL. This thing has me quite baffled.
I'm porting a program to Pocket PC 2002 using STLPort using MS
EVC3 and I keep running into this problem:
...\STLport-5.2.1\stlport\stl/_algobase.c(198) : error C2678: binary '==' : no operator defined
which takes a left-hand operand of type 'struct stlp_std::pair<class CIMGFile,bool>' (or there is no acceptable conversion)
The problem occurs in an implementation of find:
template <class _InputIter, class _Tp>
_STLP_INLINE_LOOP _InputIter __find(_InputIter __first, _InputIter __last,
const _Tp& __val,
const input_iterator_tag &) {
while (__first != __last && !(*__first == __val)) ++__first;
^ on this line the error is reported
return __first;
}

This is deep in the standard library. The actual error is due
to something incorrect in the call which instantiated this
function. (It could also be a bug in the implementation of the
standard library, but my bets are with an error in your call.)
Could you write a very small program which displays the error,
and post it with all of the error messages?

(If you can, you might also try compiling with a different
compiler/library implementation. The quality of the error
messages varies greatly from one compiler to the next, and it's
possible that the error messages from a different compiler will
be clearer to you.)
As far as I can tell, the pair is being constructed with this
macro call: std::make_pair(CIMGFile(), true)
CIMGFile is a proper class that seems to compile ok.
Previously, in a header file, the programmer defined the ==
operator using:
inline bool operator == (std::pair<CIMGFile, bool> & p, int id )
{
return p.first.GetID() == id;
}
So STLPort keeps telling me that there is no == operator while
there clearly should be. Note that it doesn't work either if I
change std::pair to stlp_std::pair or make it const in the
operator definition. I have also tried "int & id" instead with
no effect. Is there something wrong about the type signature
that I am missing? How can I get this operator definition to
work?

Probably, the first argument of the operator== must be a
reference to const, although if you're passing in non-const
iterators to a standard container, it should still work. Still,
without seeing a concrete example, it's hard to tell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top