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
2^3^4would mean (2^3)^4 = 4096, rather than the more usual 2^(3^4) ~= 2.4E24. - Philip C2^3^4without brackets is a bad idea no matter what the associativity is. Every coder that doesn't know associativity rules by heart will doubt what it actually does. - KillianDS