运算符重载参数问题
我想重载“^”符来实现一个数求n次方,这是我的代码:#include <iostream>
using namespace std;
double operator ^(double &a,int &exp)//exp为指数
{
int i=1;
double sum=a;
for(i;i<exp;i++)
{
sum=sum*a;//求a的exp次方
}
return sum;
}
void main()
{
double a=2;
double b=0;
b=a^2;
cout<<b<<endl;
}
错误如下:
error C2803: 'operator ^' must have at least one formal parameter of class type
error C2296: '^' : illegal, left operand has type 'double'
请大神帮忙解释下