user13290696
user13290696

Reputation:

How can I define == operator when comparing vectors of classes and objects?

I want my program to stop when vector has the same colors over 3 times. Here, I used 'if(b==all_colors[i])' but I'm getting the error. Is this because I didn't use the template ? Do I have to rewrite the whole code?

#include<iostream>
#include<string>
#include<sstream>
#include<vector>

using namespace std;

class Bag
{
    string marble;

public:
    Bag(string marble)
    {
        this->marble=marble;
    }

    string add_marble()
    {
        return marble;
    }
};

class Marble_exception
{
    vector<Bag>all_colors;
    int count=0;

public:
    void add_color(Bag b)
    {
        for(int i=0; i<all_colors.size(); i++)
        {
            if(b==all_colors[i])
            {
                count++;
            }
        }

        if(count>=3)
        {
            cout<<"Sorry, you already have three of these marbles.\n\n";
        }
        else
        {
            all_colors.push_back(b);
            cout<<"added.\n\n";
        }
    }
};

Upvotes: 1

Views: 80

Answers (4)

chakaz
chakaz

Reputation: 311

As others noted, the error is due to Bag not having an operator==. Others also showed how to implement one. However, if you're using C++20, you could have the compiler implement operator== as well as other operators (!=, <, <=, >, >=) for you, which also has an advantage if you will add members to your class (unless of course you wouldn't want them to participate in the comparison - in which case you'll need to resort to implementing these operators on your own).

Here's how I'd do it in C++20:

// Insert this inside class Bag
auto operator<=>(const Bag&) const = default;

Upvotes: 2

Farhad Sarvari
Farhad Sarvari

Reputation: 1081

You can use from a map that define class Bag as key to the number of colors.

map< Bag , int> Marble;

But you should define operator < for class Bag as follow :

bool operator <(const Bag& inBag) const
{
    return(marble.compare(inBag.marble) < 0);
}

Upvotes: 0

6502
6502

Reputation: 114539

C++ doesn't define automatically an == operator when you define a class. There is no specific reason, but this is the situation.

If you want to be able to tell if a Bag is equal or not to another Bag you need to tell how to perform this test by defining an bool operator==(const Bag& other) const method in your class or a suitable free operator overload.

Note that even if you explain how to check if a == b for your class instances the compiler will not infer how to tell if a != b automatically; that is also something you need to do explicitly. Once again don't look too hard for a reason for that, it's just the way C++ currently is.

There should be a simplification for creating all comparison methods at once in C++20 with the new "spaceship" operator <=>.

Upvotes: 2

john
john

Reputation: 87957

No you don't have to rewrite the whole code. The error is because there is no operator== for your Bag class. C++ knows how to compare vector<T> but only if it also knows how to compare T. Add this to your code

class Bag
{
    ...
    // new code
    friend bool operator==(const Bad& x, const Bag& y)
    {
        return x.marble == y.marble;
    };
    // new code
};

This code defines operator== for Bag so now there should be no problems using == for vector<Bag>.

Upvotes: 2

Related Questions