"匹配这些操作数-6ren"> "匹配这些操作数-我在编写这个程序时遇到了一些问题。我相信我需要重载 > 运算符,但我不确定该怎么做。有人可以帮我解释一下吗? 到目前为止我的代码(不完整): #include #include //Include-6ren">
gpt4 book ai didi

c++ - 没有运算符 ">"匹配这些操作数

转载 作者:行者123 更新时间:2023-11-28 07:17:51 25 4
gpt4 key购买 nike

我在编写这个程序时遇到了一些问题。我相信我需要重载 > 运算符,但我不确定该怎么做。有人可以帮我解释一下吗?

到目前为止我的代码(不完整):

#include <iostream>
#include <fstream> //Includes the file stream for inputting and outputting files.
#include <string> //Fixes the error i get with some operands.
#include <iomanip> //Helps with report formatting.

using namespace std;

void sort(record A[], int size, int sortField);

struct record
{
string CustId, SPID, CustLN, CustFN;
int Q1;
double P1;
int Q2;
double P2;
int Q3;
double P3;
string LOD;
bool ShipRec;
string NCD, PreMethod;
double TotalSales;
};

int main()
{
ifstream InFile("master.txt");
ofstream OutFile1("report1.txt");
ofstream OutFile2("report2.txt");
ofstream OutFile3("report3.txt");
ofstream OutFile11("report11.txt");
ofstream OutFile13("report13.txt");
ofstream OutFile15("report15.txt");

record Customer[100];
while (!InFile.eof())
{
int i = 0;

InFile >> Customer[i].CustId >> Customer[i].SPID >> Customer[i].CustLN >> Customer[i].CustFN >> Customer[i].Q1 >> Customer[i].P1 >> Customer[i].Q2 >> Customer[i].P2 >> Customer[i].Q3 >> Customer[i].P3 >> Customer[i].LOD >> Customer[i].ShipRec >> Customer[i].NCD >> Customer[i].PreMethod;

Customer[i].TotalSales = (Customer[i].Q1 * Customer[i].P1) + (Customer[i].Q2 * Customer[i].P2) + (Customer[i].Q3 * Customer[i].P3);


}


}

void sort(record A[], int size, int sortField)
{
if (sortField = 1)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}

else if (sortField = 2)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}

else if (sortField = 3)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}

else if (sortField = 11)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}

else if (sortField = 13)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}

else if (sortField = 15)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (A[j] > A[j + 1])
{
record temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}

}

最佳答案

排序字段是您函数的参数,因此您不能编写常规的 operator< ,因为它无法看到该参数的值。 (除非你让它全局化。不要那样做)。

假设没有特殊原因您必须编写自己的O(n2) 算法,最简单的解决方案是:

class SortByField {
int sortField_;
public:
explicit SortByField(int f) : sortField_(f) {}
// default copy ctor is fine
bool operator() (record const &a, record const &b) const;
};

void sort(record A[], int size, int sortField) {
std::sort(A, A+size, SortByField(sortField));
}

现在,剩下的就是编写函数调用运算符。我猜它应该看起来像这样:

bool SortByField::operator() (record const &a, record const &b) const {
switch (sortField_) {
case 1: return a.CustID < b.CustID;
case 2: return a.SPID < b.SPID;
// ...
default: return false; // maybe we should check sortField is valid before this?
}
}

关于c++ - 没有运算符 ">"匹配这些操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942420/

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