我正在编写一个程序,它将读取带有社会安全号码(当然不是真实号码)的姓名列表,并根据命令行参数根据姓氏或 ssn 对列表进行排序。为了简单起见,我重载了 < 运算符,还重载了输入和输出运算符。一切编译正常,直到我在 main 末尾添加排序函数和输出。我难住了。有任何想法吗?任何其他提示也非常感谢。
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <fstream>
using namespace std;
enum sortVar { NAME, SOCSEC };
class record {
public:
friend bool operator<(record& rhs, record& name);
friend ostream& operator<<(ostream& out, record& toWrite);
friend istream& operator>>(istream& in, record& toRead);
bool t_sort;
private:
string firstName, lastName, ssn;
};
bool operator<(record& rhs, record& next)
{
if (rhs.t_sort = false) {
if (rhs.lastName == next.lastName)
return rhs.firstName < next.firstName;
else
return rhs.lastName < next.lastName;
}
else if (rhs.t_sort = true)
return rhs.ssn < next.ssn;
}
ostream& operator<<(ostream& out, record& toWrite)
{
out << toWrite.lastName
<< " "
<< toWrite.firstName
<< " "
<< toWrite.ssn;
}
istream& operator>>(istream& in, record& toRead)
{
in >> toRead.lastName >> toRead.firstName >> toRead.ssn;
}
int main(int argc, char* argv[])
{
if (argc !=3) {
cerr << "Incorrect number of arguments.\n";
exit(1);
}
if (argv[1] != "name" || argv[1] != "socsec") {
cerr << "Argument 1 must be either 'name' or 'socsec'.\n";
exit(1);
}
sortVar sortMode;
if (argv[1] == "name")
sortMode = NAME;
else if (argv[1] == "socsec")
sortMode = SOCSEC;
ifstream fin(argv[2]);
vector<record> nameList;
while(!fin.eof()) {
record r;
if (sortMode == NAME)
r.t_sort = false;
else if (sortMode == SOCSEC)
r.t_sort = true;
fin >> r;
nameList.push_back(r);
}
//sort(nameList.begin(), nameList.end());
//cout << nameList;
}