简体   繁体   中英

c++ struct operator overload not working

I have a vector of pointers to structs, and want to check for existing items and sort by the value of a struct member. However, it appears that the check for existing items (I'm using QVector::indexOf()) is not working, and that std::sort is applying its sorting to some pointer value rather than the member value. I don't understand why the struct operator overloads aren't getting called (or called properly).

In header file:

struct PlotColumn {
    quint32 octagonLength;
    QVector<Qt::GlobalColor> columnVector;

    bool operator< (const PlotColumn& pc) const
    {
        return (this->octagonLength < pc.octagonLength);
    }
    bool operator==(PlotColumn const& pc)
    {
        return this->octagonLength == pc.octagonLength;
    }
    bool operator> (const PlotColumn& pc) const
    {
        return (this->octagonLength > pc.octagonLength);
    }
};
QVector<PlotColumn*> *plotMap;

In source file:

PlotColumn *pcNew = new PlotColumn();
pcNew->octagonLength = octagonLength;
// check if octagonLength has arrived before:
int plotXAxis = plotMap->indexOf(pcNew);
if (plotXAxis == -1) { // unknown, so insert:
     ... (some housekeeping)
     plotMap->append(pcNew);
     std::sort(plotMap->begin(), plotMap->end());
     ....
     (some more housekeeping)
}

Using an external (not in the struct) is possible for sort, but not for indexOf (afaik).

Any thoughts?

PS: yes I know there are tons of already answered questions about sorting vectors of pointers to structs and I've tried to apply the pattern in those solutions but it still doesn't work.

You need to provide a comparater to std::sort() that works with pointers.

bool PlotColumnPointerComparator(const PlotColumn *a, const PlotColumn *b)
{
     return (*a) < (*b);   // this will call your PlotColumn:: operator<()
}

And change your std::sort() statement to

std::sort(plotMap->begin(), plotMap->end(), PlotColumnPointerComparator);

In C++11, you could do the above with a lambda function, but that's just a syntactic convenience.

Compilers are not omnipotent mindreaders. If you tell it to sort a set of pointers, it will do pointer comparison. If you want the comparison to dereference the pointers and compare the pointed-to objects, you need to tell it to do that .... as in the above.

std::sort is applying its sorting to some pointer value

Let's see:

QVector<PlotColumn*> *plotMap

std::sort(plotMap->begin(), plotMap->end());

Yep, that's exactly what you told std::sort to do (assuming QVector resembles vector ): you have a container of pointers, told it to sort the elements of the container. Thus, you told it to sort pointers, not PlotColumn objects.

If you want to sort the pointers based on how the objects they point to compare, you have to apply one of those

answered questions about sorting vectors of pointers to structs

You've already identified your problem:

I've tried to apply the pattern in those solutions but it still doesn't work.

Don't give up on that line of inquiry: you've correctly identified something you need to work on, and should work to understand how those patterns work and how to apply them to your problem, and maybe ask a focused question regarding your attempts at that.

It will not work because You are using the same signature. Overload works for different signatures. Check your function signatures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM