gpt4 book ai didi

C++ 排序函数

转载 作者:行者123 更新时间:2023-11-30 02:05:49 26 4
gpt4 key购买 nike

因此,我正在尝试让排序功能发挥作用。它应按名字排序,但如果名字相同,则应按姓氏排序。我不断在 mySort() 中收到错误,在 inOrder = arr 的行上显示“')' 标记之前的意外主表达式”。这是怎么回事,我该如何解决?我需要将我的对象传递给单独类中的函数。我会在 pastebin 中链接它们。

这是司机

#include <iostream> 
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;

void mySort(PhoneEntry& arr, int size)
{
bool inOrder = false;
for (int i = size - 1; i > 0 && !inOrder; i--)
{
inOrder = true;
for (int j = 0; j < i; j++)
{
inOrder = arr.alphaGreater(arr&);
}
}
};

int main()
{
const int MAXNUM = 500;
PhoneEntry entry[MAXNUM];
ifstream filezilla;
filezilla.open("phone.txt");
int count = 0;

if(filezilla)
{
while(count < MAXNUM && entry[count].readEntry(filezilla))
{
count++;
}
mySort(entry&, count);
for(int i = 0; i < count; i++)
{
entry[i].writeEntry(cout) << endl;
}
}
else
{
cout << "Four Oh Four - File Not Found" << endl;
}

return 0;
}

Phone Entry Header

Phone Number Header

排序文本 (http://pastebin.com/HE8Rsmbg)

最佳答案

你想传递一个引用,所以参数必须在函数定义/声明中定义为引用调用,这已经完成了:

bool PhoneEntry::alphaGreater(const PhoneEntry& item) const;

如您所见,PhoneEntry::alphaGreater 采用常量 PhoneEntry reference。所以简单地使用

inOrder = arr.alphaGreater(arr);

关于C++ 排序函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9373787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com