1

I have a struct with the operator== overloaded.

It works fine with the first condition, but for the second condition it isn't working at all. What am i doing wrong?

Here is my code:

struct simpleLink {
    int orig;
    int dest;
    set<int> commonKeys;
    bool captured;
    bool compromised;

    bool operator<(const simpleLink& otherLink) const
    {
       if (orig == otherLink.orig) {
            return dest < otherLink.dest;
        }

        return orig < otherLink.orig;
    }

    bool operator==(const simpleLink& otherLink) const
    {
        return ((orig == otherLink.orig) && (dest == otherLink.dest)) || ((orig == otherLink.dest) && (dest == otherLink.orig));
    }

};

Main:

int main() {

    set<simpleLink> test;

    simpleLink secureLink;
    secureLink.orig = 0;
    secureLink.dest = 1;

    simpleLink secureLink2;
    secureLink2.orig = 1;
    secureLink2.dest = 0;

    test.insert(secureLink);
    test.insert(secureLink2);

    cout << secureLink.orig << " " << secureLink.dest << endl;
    cout << secureLink2.orig << " " << secureLink2.dest << endl;

    cout << "Test Size:" << test.size() << endl;

    return 0;
}

Output is:

0 1
1 0
Test Size: 2

The size should be 1.

The question now is, how can i change the operator< so (0, 1) = (0, 1) and (0, 1) = (1 , 0)

  • `The size should be 1`, why?? – Swastik Padhi Oct 25 '15 at 22:02
  • Interesting. Should be 1 because the == operator is true and thus it should be the same object and only be inserted into the set once – anhoppe Oct 25 '15 at 22:04
  • @anhoppe Where is the `==` operator?? The OP has of-course overloaded the operators `<` and `==` but I don't see them being used anywhere. What has `test.size()` to do with the operator overloading? – Swastik Padhi Oct 25 '15 at 22:09
  • Um, operator==(const& simple link) ? But as the linked question states, this is not the criteria that is used by std::set. Thus this is really a duplicate. – anhoppe Oct 25 '15 at 22:12
  • Yeah, i see now that the criteria is operator<(), but how can i make it? In my logic (0, 1) = (0, 1) and (0, 1) = (1 , 0) –  Oct 25 '15 at 22:20

0 Answers0