简体   繁体   English

二进制运算符重载C ++

[英]Binary Operator Overloading C++

I'm trying to overload the following operators to sort a string array using a Quick Sort or possibly Merge Sort algorithm. 我正在尝试重载以下运算符,以使用“快速排序”或可能的“合并排序”算法对字符串数组进行排序。 I'm have all my functions in a single class but I'm getting a "too many parameters for this operator function" error. 我将所有函数都放在一个类中,但是出现“此运算符函数的参数太多”错误。 Indeed, it will only accept one parameter. 实际上,它将仅接受一个参数。 I looked up the problem and in a forum someone said that you can only use one parameter when overloading an operator inside a class. 我查了一下问题,在论坛上有人说您只能在类中重载运算符时使用一个参数。 This doesn't make much sense to me. 这对我来说没有多大意义。 I'm trying to compare strings so I need the two parameters for the overloading. 我正在尝试比较字符串,因此我需要两个参数来进行重载。 Am I supposed to overload the operators outside the class, and how would this work? 我应该让课外的操作员超负荷吗,这将如何工作?

Here's my code: 这是我的代码:

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

The signatures for the operators are incorrect: 运算符的签名不正确:

bool operator<=(string a, string b);
bool operator<(string a, string b);
bool operator>(string a, string b);
  1. When you overload an operator - and you implement it as a member function, it should only accept one argument (the other thing to compare to) 当重载操作员-你实现它作为一个成员函数,它应该只接受一个参数(其他的事情来比较)
  2. If non-member function (ie friend), then you can provide two arguments, however it cannot match an exiting operator (there is one already defined for std::string ), and typically should accept your class as lhs and rhs for testing. 如果是非成员函数(例如,朋友),则可以提供两个参数,但是它不能与现有的运算符匹配(已经为std::string定义了一个),通常应将您的类接受为lhs和rhs进行测试。

An operator inside a class, whatever it is, has the special meaning of applying that operator to an instance of that class and optionally to parameters. 一个operator一个类的内部,不管它是什么,有应用该操作到类的一个实例并任选参数的特殊意义。

In your example the operator<= is supposed to compare an instance of the Preprocessing class with a string . 在您的示例中, operator<=应该将Preprocessing类的实例与string

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

private:
    string aStringField;
}

Typically you use this inside the operator method body to compare the instance with the parameter: 通常,您可以在运算符方法主体中使用this函数,以将实例与参数进行比较:

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

And you call it with: 您可以这样称呼:

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

Which is equivalent to: 等效于:

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

If you want to provide an operator that doesn't need the "point syntax" to be called, then you're looking for friend operators that exist outside your class. 如果要提供不需要调用“点语法”的运算符,那么您正在寻找存在于类之外的friend运算符。

class Preprocessing
    {
    public:
        friend ostream& operator<<(ostream&, const Preprocessing&);

    private:
        string aStringField;
    }

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

To overload an operator, the operator needs to be a method of the lefthand operand. 要使运算符重载,运算符必须是左操作数的方法。 C++ chooses functions (and operators) based on the types of the arguments (operands). C ++根据参数(操作数)的类型选择函数(和运算符)。 Within a class, the lefthand operand is an instance of the class, available as the this pointer, so only the righthand operand may be specified as an argument to the operator. 在一个类中,左操作数是该类的实例,可用作this指针,因此只能将右操作数指定为运算符的参数。

In your example, you could do this: 在您的示例中,您可以这样做:

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

which would define a <= operator for comparing Preprocessing objects to strings. 它将定义一个<=运算符,用于将Preprocessing对象与字符串进行比较。 If you need to overload string comparison operators, you need to modify the std::string class, which is beyond my knowledge. 如果需要重载字符串比较运算符,则需要修改std::string类,这是我所不了解的。

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

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