Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I'm having some difficulties when trying to comapare two deques of a selfmade struct.
This is my struct and the deque declaration:
class iterator
{
public:
struct PosPtrs
{
    NodeLevel Level;
    TreeElement Element;
    bool operator ==(const PosPtrs& input)
    {
        return Level == input.Level && Element == input.Element;
    }
    bool operator== (PosPtrs& input)
    {
        return Level == input.Level && Element == input.Element;
    }
};
std::deque<PosPtrs> TreeHistory;
private:
....
};
 
When Trying
 
iterator it1;
iterator it2;
return it1.TreeHistory == it2.TreeHistory;
I get a compiler error C2678:

d:\program files\microsoft visual studio 10.0\vc\include\xutility(2990):error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const BaseTree::iterator::PosPtrs' (or there is no acceptable conversion)
1> d:\program files\microsoft sdks\windows\v7.0a\include\guiddef.h(192): could be 'int operator ==(const GUID &,const GUID &)' [found using argument-dependent lookup]
1> d:\program files\microsoft visual studio 10.0\vc\include\exception(470): or 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1> d:\program files\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1> d:\program files\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1> d:\program files\microsoft visual studio 10.0\vc\include\system_error(408): or 'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1> d:\program files\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1> d:\users\maxime\documents\visual studio 2010\projects\regfilehook\regfilehook\xmlfilereadwrite.h(156): or 'bool BaseTree::iterator::PosPtrs::operator ==(const BaseTree::iterator::PosPtrs &)'
1> d:\users\maxime\documents\visual studio 2010\projects\regfilehook\regfilehook\xmlfilereadwrite.h(160): or 'bool BaseTree::iterator::PosPtrs::operator ==(BaseTree::iterator::PosPtrs &)'
1> while trying to match the argument list '(const BaseTree::iterator::PosPtrs, const BaseTree::iterator::PosPtrs)'
1> ....

 
The last lines are confusing me. The compiler tries to match an argument list of a operator++() function with 2 arguments? I tought an operator== was always defined with only one argument (trying this doesn't helps either, it ends like expected in a compiler error)..
 
Thanks in Advance
Posted 21-Aug-12 8:57am
MXT3389
Edited 21-Aug-12 10:24am
v3
Comments
Sergey Alexandrovich Kryukov at 21-Aug-12 14:06pm
   
Always indicates the line(s) related to the compiler error message in the source code sample. Do you like people to waste time?
--SA
Sergey Alexandrovich Kryukov at 21-Aug-12 14:08pm
   
How a binary infix operator ("==" or some other) can "one argument"?! The problem is not that you don't know something; I'm just curious what was your thinking?
--SA
MXT3 at 21-Aug-12 15:11pm
   
If you take for instance two int a and b; then "a == b" is the same as "a.operator==(b)", or not?
Sergey Alexandrovich Kryukov at 21-Aug-12 16:37pm
   
That's too parameters, a and b. You think in a.Operator syntax a is something special, but this is just another parameter. "==" is binary.
In C++, the operator's function is a non-static (instance) function (in some other systems, the developers supplied static function with two parameters, one must be of the same class), but the mechanism of some function is passing, say, a and a hidden/implicit parameter to the function as a regular parameter. Inside implementation, this parameters is accessible as "this". Are you getting the idea..?
--SA
MXT3 at 22-Aug-12 4:39am
   
Yes "a" is nothing special, it's the parent of it's operator== function and is passed as a implicit parameter. The words " argument list (const BaseTree::iterator::PosPtrs, const BaseTree::iterator::PosPtrs)" just confused me because it looked like the compiler was looking for two explicit parameters, now I know the first one is implicit, and that it had to be passed const
Sergey Alexandrovich Kryukov at 22-Aug-12 11:25am
   
Are you unconfused now? :-)
--SA
MXT3 at 22-Aug-12 14:39pm
   
yes, thank you :)
Sergey Alexandrovich Kryukov at 22-Aug-12 17:49pm
   
Great! Good luck, call again.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

class iterator
{
public:
struct PosPtrs
{
    NodeLevel Level;
    TreeElement Element;
    bool operator ==(const PosPtrs& input) const
    {
        return Level == input.Level && Element == input.Element;
    }
};
std::deque<posptrs> TreeHistory;
private:
....
};
 
The clue comes from this message:
d:\program files\microsoft visual studio 10.0\vc\include\xutility(2990):error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const BaseTree::iterator::PosPtrs' (or there is no acceptable conversion)
 
Your left hand operand needs to be const also.
  Permalink  
v3
Comments
MXT3 at 21-Aug-12 16:10pm
   
Wonderful, thank you very much
Sergey Alexandrovich Kryukov at 22-Aug-12 11:26am
   
I hope OP in unconfused this time (please see the discussion in comments to the question), my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 280
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 120
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 23 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100