我需要一个简单的运算符重载示例。不使用类或结构。在这里我尝试过但收到错误:
#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;
}
我收到以下错误:
Compiling OVERLOAD.CPP:
Error OVERLOAD.CPP 3: 'operator +(int,int)' must be a member function or have a parameter of class type
让我知道是否可以重载运算符 (sum=a+b) ?如果是,请在我的来源中进行更正。