看看我的运算符重载哪里错了,运行不了
#include<iostream>using namespace std;
class A
{
private:
int n;
int *p;
friend A operator+(A &a1, A &a2); //友元函数
public:
A(int i = 0) //构造函数 为n赋值 为指针p定义元素个数
{
if (i < 0)
{
i = 0;
}
n = i;
p = new int[n];
}
~A() //析构函数
{
if (p != NULL)
delete[]p;
}
void SetA() //赋值
{
for (int i = 0; i < n; i++)
p[i] = i;
}
void Display() //打印
{
for (int i = 0; i < n; i++)
cout << p[i] << " ";
cout << endl;
}
};
A operator+(A &a1, A &a2) //运算符+号重载
{
A temp(10);
for (int i = 0; i < a1.n && i<a2.n; i++)
{
temp.p[i] = a1.p[i] + a2.p[i];
}
return temp;
}
int main()
{
A b1(10), b2(10);
b1.SetA(); //赋值
b2.SetA(); //赋值
A b3(10); //定义b3的p指针的int元素个数
b3 = b1 + b2; //程序到这里就要响一下声音 就没反应了
b1.Display();
b2.Display();
b3.Display();
system("pause");
return 0;
}