C++中的运算符重载示例

use*_*703 2 c++

我需要一个简单的运算符重载示例.不使用类或结构.在这里,我尝试但得到错误:

#include <iostream.h>
int operator+(int a, int b)
{
  return a-b;
}

void main()
{
  int a,b,sum;
  cin>>a>>b;
  sum=a+b;  //Actually it will subtruct because of + operator overloading.
  cout<<"Sum of "<<a<<" & "<<b<<"(using overloading)"<<sum;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Compiling OVERLOAD.CPP:
Error OVERLOAD.CPP 3: 'operator +(int,int)' must be a member function or have a parameter of class type
Run Code Online (Sandbox Code Playgroud)

让我知道是否有可能重载运算符(sum = a + b)?如果是,那么请在我的来源中进行更正.

Jes*_*sak 8

无法覆盖像int这样的基本类型的运算符.正如编译器所述,至少一个参数的类型必须是一个类.