3

演算子のオーバーロードの簡単な例が必要です。クラスまたは構造体を使用せずに。ここで私は試しましたが、エラーが発生しました:

#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

operator (sum=a+b) をオーバーロードすることは可能ですか? はいの場合は、ソースで修正してください。

4

3 に答える 3