繁体   English   中英

二进制运算符重载C ++

[英]Binary Operator Overloading C++

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

这是我的代码:

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

运算符的签名不正确:

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

一个operator一个类的内部,不管它是什么,有应用该操作到类的一个实例并任选参数的特殊意义。

在您的示例中, operator<=应该将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;
    }

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

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

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

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

它将定义一个<=运算符,用于将Preprocessing对象与字符串进行比较。 如果需要重载字符串比较运算符,则需要修改std::string类,这是我所不了解的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM