Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I'm having some difficulties when trying to comapare two deques of a selfmade struct.
This is my struct and the deque declaration:
C++
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
C++
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
Updated 21-Aug-12 9:24am
v3
Comments
Sergey Alexandrovich Kryukov 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 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 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 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 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

1 solution

C++
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.
 
Share this answer
 
v3
Comments
MXT3 21-Aug-12 16:10pm    
Wonderful, thank you very much
Sergey Alexandrovich Kryukov 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900