Overload operator ^ for integer type

I wrote an application in which I often use a function powfrom the library math.h. I tried to overload operator^to simplify and speed up the exposure. 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);
}

The compiler (I used g ++ on Linux) returned these errors to me:

main.cpp: 6: 23: error: 'int operator ^ (int, int) must have an argument to the class or enumerated type main.cpp: 21: 27: error:' int operator ^ (int, int) must have an argument to the class or enumerated type

+3
source share
2 answers

You cannot overload an operator to work only for built-in types .

, ( ), .

+9

^, .

, , , . , , , .

, ++ , , .

+6

All Articles