【问题标题】:Binary Operator Overloading C++二元运算符重载 C++
【发布时间】:2012-03-24 05:01:39
【问题描述】:

我正在尝试重载以下运算符以使用快速排序或可能的合并排序算法对字符串数组进行排序。我的所有函数都在一个类中,但出现“此运算符函数的参数过多”错误。事实上,它只会接受一个参数。我查了这个问题,在论坛上有人说在类中重载运算符时只能使用一个参数。这对我来说没有多大意义。我正在尝试比较字符串,所以我需要两个参数来进行重载。我是否应该重载类外的运算符,这将如何工作?

这是我的代码:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Preprocessing
{

public:

void readFile(string list[], int size);
void quickSort(int list[], int lowerBound, int upperBound);
void swapItem(int &a, int &b);

//These are the overloading functions I'm trying to implement
bool operator<=(string a, string b);
bool operator<(string a, string b);
bool operator>(string a, string b);
};

void Preprocessing::readFile(string list[], int size)
{
ifstream myFile;
myFile.open("words.txt");

for (int i = 0; i < size; i++)
{
    myFile >> list[i];
}

myFile.close();
}

void Preprocessing::quickSort(int list[], int lowerBound, int upperBound)
{
    int i, j, pivot;

    i = lowerBound;
    j = upperBound;

    pivot = list[(i + j) / 2];

    while (i <= j)
    {
        while(list[i] < pivot)
        {
            i = i + 1;
        }
        while (list[j] > pivot)
        {
            j = j - 1;
        }
        if (i <= j)
        {
            swapItem(list[i], list[j]);
            i = i + 1;
            j = j - 1;
        }//end if
    }//end outter while
    if (lowerBound < j)
    {
        quickSort(list, lowerBound, j);
    }
    if (i < upperBound)
    {
        quickSort(list, i, upperBound);
    }//end recursive if
}//end function

void Preprocessing::swapItem(int &a, int &b){
    int tmp;

    tmp = a;
    a = b;
    b = tmp;
}

bool Preprocessing::operator<=(string a, string b)
{
if (a.compare(b) > 0)
    return false;
else if (a.compare(b) == 0)
    return true;
else
    return true;
}

bool Preprocessing::operator<(string a, string b)
{
if (a.compare(b) > 0)
    return false;
else if (a.compare(b) == 0)
    return true;
else
    return true;
}

bool Preprocessing::operator>(string a, string b)
{
if (a.compare(b) > 0)
    return false;
else if (a.compare(b) == 0)
    return true;
else
    return true;
}

【问题讨论】:

  • 您是否尝试替换标准运算符来比较std::string?或者您是否想让您的类型与std::string 相媲美?

标签: c++ operator-overloading


【解决方案1】:

运算符的签名不正确:

bool operator<=(string a, string b);
bool operator<(string a, string b);
bool operator>(string a, string b);
  1. 当您重载一个运算符 - 并将其实现为成员函数时,它应该只接受一个参数(要比较的另一个 事物
  2. 如果是非成员函数(即朋友),那么您可以提供两个参数,但是它不能匹配现有的运算符(已经为 std::string 定义了一个),并且通常应该接受您的类作为 lhs 和 rhs测试。

【讨论】:

  • 哦,好的。那么声明第一个参数是多余的吗?
  • @rocklandcitizen - 你明白了。
  • @rocklandcitizen 不是多余的,不必要的。左侧操作数(“第一个参数”)是您的类的一个实例,可以通过 this 指针访问。
【解决方案2】:

它只需要一个参数,因为左边是作为this 指针传递的。

【讨论】:

    【解决方案3】:

    要重载运算符,运算符需要是左操作数的方法。 C++ 根据参数(操作数)的类型选择函数(和运算符)。在一个类中,左操作数是该类的一个实例,可用作this 指针,因此只能将右操作数指定为运算符的参数。

    在您的示例中,您可以这样做:

    class Preprocessing {
        public:
            bool operator<=(string b);
    };
    

    这将定义一个&lt;= 运算符,用于将Preprocessing 对象与字符串进行比较。如果需要重载字符串比较操作符,需要修改std::string类,这个我不知道。

    【讨论】:

      【解决方案4】:

      类中的operator,无论它是什么,都具有将该运算符应用于该类的实例和可选参数的特殊含义。

      在您的示例中,operator&lt;= 应该将Preprocessing 类的实例与string 进行比较。

      class Preprocessing
      {
      public:
          bool operator<=(string a);
      
      private:
          string aStringField;
      }
      

      通常你在操作符方法体内使用this来比较实例和参数:

      bool Preprocessing::operator<=(string a)
      {
         return this->aStringField.length() <= a.length();
      }
      

      你可以这样称呼它:

      Preprocessing p;
      if ( p <= "a string" )
          // ...
      

      相当于:

      Preprocessing p;
      if ( p.operator<=("a string") )
          // ...
      

      如果您想提供一个不需要调用“点语法”的运算符,那么您正在寻找存在于您的类之外的friend 运算符。

      class Preprocessing
          {
          public:
              friend ostream& operator<<(ostream&, const Preprocessing&);
      
          private:
              string aStringField;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-20
        • 2011-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-31
        相关资源
        最近更新 更多