1

it seems that overloading operators is not working for me, I am probably doing something wrong.

I've got this structure

typedef void (*fptr)(void);
struct Mystruct
{
    int id;
    int priority;
    double timeSaved;
    double aktivationTime;
    fptr eventPointer;
    MystructnextPtr;

    Mystruct(int id, int priority, double timeSaved, double aktivationTime, fptr eventPointer);
};

This operator:

bool operator <(const Mystruct& x, const Mystruct& y)
{
    return std::tie(x.aktivationTime, x.priority, x.timeSaved) < std::tie(y.aktivationTime, y.priority, y.timeSaved);
}

And am trying to execute this code:

Mystruct* struct1 = new Mystruct(5, 10, 0, 15.3, f1);
Mystruct* struct2 = new Mystruct(5, 10, 0, 14.3, fi);

if (struct1 < struct2)
{
    cout << "struct1 is smaller!" << endl;
}
else
{
    cout << "struct2 is smaller!" << endl;
}

It ALWAYS says to me that struct1 is smaller, regardless of what is in those structures. What am doing wrong?

Dracke
  • 651
  • 2
  • 11
  • 30

1 Answers1

0

You created an operator handling constant references to Mystruct. (const Mystruct&). However you are using the comparator operator on non-const pointers to non-const MyStruct instances.

You have one real option:
- deference your pointers (*struct1 < *struct2)

However also take a look here.. C++: Operator overloading of < for pointers to objects

Community
  • 1
  • 1
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46