我需要一个简单的运算符重载示例.不使用类或结构.在这里,我尝试但得到错误:
#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)?如果是,那么请在我的来源中进行更正.