简体   繁体   中英

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.

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.

In your example the operator<= is supposed to compare an instance of the Preprocessing class with a 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:

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.

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). 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.

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. If you need to overload string comparison operators, you need to modify the std::string class, which is beyond my knowledge.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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