没有运营商“>”匹配这些操作数

时间:2013-11-12 23:50:11

标签: c++

我正在写这个程序有些麻烦。我相信我需要重载>运营商,但我不知道该怎么做。有人可以为我阐明这个吗?

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

#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;
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

sort字段是函数的参数,因此您无法编写常规operator<,因为它无法查看该参数的值。 (除非你把它全局化。不要这样做。)

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

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?
  }
}

答案 1 :(得分:0)

我假设sortField选择要比较的字段。如果是这种情况,您应该将代码更改为:

if (sortField == 1) // <-- use '==' for comparisson
{
  for (int i = 0; i < size; i++)
  {
    for (int j = 0; j < size-1; j++) // <-- should probably be size-1
    {
      if (A[j].CustId > A[j + 1].CustId) // <-- test the sortField
      {
        record temp = A[j];
        A[j] = A[j + 1];
        A[j + 1] = temp;
      }
    }
  }
}

和其他sortField值相似