问题描述
我不知道如何调用我的operator
错误包括: -在函数'void selectionSort(Blog *,int)'中: -[错误]“类Blog”没有名为“ operator
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]))