-1

I've got a class using an std::vector> to indicate the item and its count (there can be multiple inventoryitems containing the same item).

I then proceeded to overload the clsInventoryItem its == operator. The setup ends up being: clsInventory.cpp -> including: clsInventory.h clsInventory.h -> including: clsInventoryItem.h clsInventoryItem.h -> including: stdafx.h (which in turns includes the rest of the project, excluding those 2 header files)

The clsInventoryItem contains the following in its header file:

class clsInventoryItem
{
public:
    clsInventoryItem( clsItem* Item, char Quality );
    clsItem* GetItem( );
    char GetQuality( );

    inline bool operator==( const clsInventoryItem& other )
    { /* do actual comparison */
        if (m_Item == other.m_Item
             && m_Quality == other.m_Quality)
        {
            return true;
        }
        return false;
    }
private:
    clsItem* m_Item;
    char m_Quality;
};

And it still gives an error that the equals function isn't overloaded ("Severity Code Description Project File Line Suppression State Error C2678 binary '==': no operator found which takes a left-hand operand of type 'const BrawlerEngineLib::clsInventoryItem' (or there is no acceptable conversion) BrawlerEngineLib d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.25017\include\utility 290 ")... Anyone knows why this could be the case, and how to potentially solve it?

18
  • And it still gives an error that the equals function isn't overloaded Please edit your question and add the text of the exact error message. If this is Visual Studio copy the error from the Output Tab. Commented Oct 2, 2017 at 21:47
  • You should not include stdafx.h from a header. The include of stdafx.h must be the first non comment line of a source file. All lines above #include "stdafx.h" are ignored by the compiler. Commented Oct 2, 2017 at 21:47
  • 2
    if (m_Item == other.m_Item -- You are comparing pointer values. Surely an m_Item's pointer value should not be used to see if two clsItem objects are equal. Commented Oct 2, 2017 at 21:56
  • 1
    The bool operator==() function should be const. Commented Oct 2, 2017 at 21:59
  • If you haven't already, ask yourself, "Who is responsible for managing m_Item?" Commented Oct 2, 2017 at 22:33

1 Answer 1

1

Your inline bool operator==(const clsInventoryItem& other) is expected to be const.
To fix that, you need to change inline bool operator==(const clsInventoryItem& other) to inline bool operator==(const clsInventoryItem& other) const.

Also, you can get rid off the keyword inline, modern compilers will ignore the keyword and older compilers use it as a hint only and decide whether or not to inline the function on their own. They are pretty good at it ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

@JoeyvanGangelen No problem, glad it helped! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.