如何在涉及 class 的 C++ 函数中进行 selectionSort,并为数组重载操作 <?

How can I make a selectionSort in C++ function involving a class, and with an overload of operation < for an array?

I do not know how to how to call my operator< function into my selectionSort function. The selectionSort function is supposed to arrange the blog objects in the array from newest to oldest. The operator< function is comparing the days elapsed since the blog post of each blog. I could use some help on how to set up my selectionSort function which calls the operator< function.

Errors include: -In function 'void selectionSort(Blog*, int)': -[Error] 'class Blog' has no member named 'operator<' -[Error] 'displayData' was not declared in this scope

void selectionSort(Blog blog[], int numBlogs)
{
    Blog temp;
    int minIndex=0;
    for (int i=0; i<numBlogs-1; i++)
    {
        minIndex = i;
        for (int j=i+1; j<numBlogs; j++)
        if (blog[j].operator<())
            minIndex=j;
                        //swap positions i and minIndex
        temp = blog[i];
        blog[i] = blog[minIndex];
        blog[minIndex] = temp;
        displayData(blog, numBlogs);
        
        
    }
}

    bool Blog::operator< (const Blog &right) const
        {
            if (daysElapsed() < right.daysElapsed())//comparing two objects that are in the blog[]
                return true;
           else
               return false;
        }

重载运算符的全部意义在于,您不必像函数调用那样拼写出来。

所以对于这一行:

if (blog[j].operator<())

除了缺少 operator< 的参数之外,您可以像这样比较 2 Blog 个对象:

if (blog[j] < blog[i])

如果您明确想要拼出运算符调用,您可以这样做:

if (blog[j].operator<(blog[i]))