Home >Backend Development >C++ >C++ syntax error: Overloaded operators must be member functions. How to correct it?

C++ syntax error: Overloaded operators must be member functions. How to correct it?

PHPz
PHPzOriginal
2023-08-22 08:22:441029browse

C is a popular programming language with powerful object-oriented programming capabilities. When programming in C, you may sometimes encounter some syntax errors. This article will discuss a common error, "Overloaded operator must be a member function" and provide a solution to solve the problem.

In C, operators can be overloaded to perform various operations using objects of custom classes. For example, the " " operator can be overloaded to implement addition between two custom class objects. Operator overloading can be implemented through member functions or global functions.

When we declare an operator overloaded function, we must specify whether it is a member function or a global function. If using member functions, the operator overloaded function must become a member function of the class. However, sometimes we may use a non-member function for operator overloading, and then we will receive the error message "The overloaded operator must be a member function."

To solve this error, we can convert the operator overloaded function into a member function of the class.

Suppose we want to overload the " " operator to implement the addition operation between two custom class objects. Our class is named "Number" and it has two private member variables num1 and num2. We can declare the operator overloaded function using the following code:

Number operator+(const Number& num1, const Number& num2) {
  Number sum;
  sum.num1 = num1.num1 + num2.num1;
  sum.num2 = num1.num2 + num2.num2;
  return sum;
}

In this code, we use the global function to overload the " " operator. This is wrong because we have to convert it to a member function. To do this, we need to declare it as a member function of the Number class and use the " " operator as the function name. The modified code is as follows:

class Number {
public:
  Number operator+(const Number& num) {
    Number sum;
    sum.num1 = num1 + num.num1;
    sum.num2 = num2 + num.num2;
    return sum;
  }
  
private:
  int num1;
  int num2;
};

In this modified code, we declare the operator overloaded function as a member function of the Number class and use the " " operator as the function name. Now, we can use the following code to perform addition between two Number objects:

Number num1;
num1.num1 = 1;
num1.num2 = 2;

Number num2;
num2.num1 = 3;
num2.num2 = 4;

Number sum = num1 + num2;

In this way, we can successfully overload the " " operator and perform addition between two Number objects Calculated.

In short, in C, overloaded operators must be implemented using member functions. If the current code uses a non-member function to implement operator overloading, to correct this problem, we can convert it into a member function, declare the function as a member function of the class, and use the operator as the function name.

The above is the detailed content of C++ syntax error: Overloaded operators must be member functions. How to correct it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more