I wrote an application in which I often use pow function from math.h library. I tried to overload operator^ to make exponentiation easier and faster. I wrote this code:
#include <iostream>
#include <math.h>
using namespace std;
int operator^(int, int); // line 6
int main(int argc, char * argv[]) { /* ... */ }
int operator^(int a, int n) // line 21
{
return pow(a,n);
}
Compiler (I used g++ on Linux) returned me these errors:
main.cpp:6:23: error: ‘int operator^(int, int)’ must have an argument
of class or enumerated type main.cpp:21:27: error: ‘int operator^(int,
int)’ must have an argument of class or enumerated type
You cannot overload an operator to operate only on built-in types.
At least one of the arguments must be a user-defined class or enumerated type (or a reference to one of those) as clearly stated in the compiler error message.