You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++对Person对象快速排序时出现“Invalid operands to binary expression”错误求助

Fixing "Invalid operands to binary expression ('Person' and 'Person')" When Sorting by Age

Hey there! Let's work through this issue—since you're new to C++, this is a super common pitfall when working with custom classes and sorting, so you're definitely not alone here.

Why the Error Happens

When you try to sort a collection of Person objects (like with std::sort, which uses a quicksort-like implementation under the hood), the compiler needs to know how to compare two Person instances to determine their order. By default, functions like std::sort try to use the < (less-than) operator to compare objects—but your Person class doesn't have this operator defined! That's why you get the "invalid operands" error: the compiler has no clue what it means to say personA < personB.

Two Easy Fixes

1. Overload the < Operator in Your Person Class

If you want sorting by age to be the "default" way to compare Person objects, add an overloaded < operator directly to the class. This tells the compiler exactly how to compare two Persons:

#include <string>

class Person {
public:
    std::string name;
    int age;
    double height;
    double weight;

    // Overload < to compare by age (ascending order)
    bool operator<(const Person& other) const {
        // Return true if this person's age is less than the other's
        return age < other.age;
    }
};
  • Marking the function const is important here—it promises that comparing two Persons won't modify either object, which is required for safe use with standard library functions like std::sort.

2. Pass a Custom Comparator to std::sort

If you don't want to set a default comparison (or if you sometimes need to sort by other fields like height/weight later), you can pass a lambda function directly to std::sort that defines the age-based comparison:

#include <vector>
#include <algorithm>
#include <string>

class Person {
public:
    std::string name;
    int age;
    double height;
    double weight;
};

int main() {
    std::vector<Person> people = {
        {"Charlie", 30, 175.0, 70.0},
        {"Diana", 22, 160.0, 52.0},
        {"Eve", 27, 170.0, 60.0}
    };

    // Sort using a lambda that compares ages
    std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
        return a.age < b.age; // Sort in ascending order of age
    });

    return 0;
}

This approach is flexible—you can easily change the lambda to sort by height or name later without modifying the Person class itself.

If You're Implementing Your Own Quicksort

If you wrote a custom quicksort function instead of using std::sort, the fix is similar: instead of trying to compare Person objects directly (like if (left < right)), compare their age members directly:

// Inside your quicksort partition function, for example:
if (left.age < pivot.age) {
    // Do your swap logic here
}

内容的提问来源于stack exchange,提问作者Fall0ut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:12:42