weixin_41333159 2018-10-17 01:43 采纳率: 100%
浏览 455
已采纳

用链表类完成一元多项式的加法 为什么vs在重载赋值运算符的时候断点,说head访问权限冲突?

#include "pch.h"
#include
using namespace std;
struct polynomial
{
int coe;
int exp;
polynomial link;
};
typedef struct polynomial
poly;
class polynode
{
private:
poly head;
poly rear;
int number;
void attach(int x, int y);
void connection(int x, int y, poly& p);
public:
polynode();
polynode(const polynode &p);
void readpoly();
polynode operator+(polynode &p);
polynode& operator=(const polynode& p);
void printpoly();
~polynode();
};
polynode::polynode()
{
head = new struct polynomial;
head->link = NULL;
rear = head;
number = 0;
}
polynode::polynode(const polynode &p)
{
int k = p.number;
poly temp,head,rear;
temp= (p.head)->link;
head = new polynomial;
head->link = NULL;
rear = head;
while (k--)
{

    rear->link = new polynomial;
    rear->link->coe = temp->coe;
    rear->link->exp = temp->exp;
    rear = rear->link;
    temp = temp->link;
}

}

void polynode::attach(int x, int y)
{
poly p;
p = new struct polynomial;
p->coe = x;
p->exp = y;
rear->link = p;
rear = p;
number++;
}
void polynode::connection(int x, int y, poly& p)
{
poly t;
t = new struct polynomial;
t->coe = x;
t->exp = y;
t->link = p->link;
p->link = t;
number++;
}
//读入函数试下来因该是没有问题的
void polynode::readpoly()
{
int num,ix,iy;
poly p;
cout << "有多少项?" << endl;
cin >> num;
cout << "请输入:" << endl;
cin >> ix >> iy;
attach(ix, iy);
num--;
while (num--)
{
cin >> ix >> iy;
p = head->link;
if (p->exp == iy)
{
p->coe += ix;
p->exp = iy;
}
while (p->exp > iy)
{
if (p == rear)
{
attach(ix, iy);
break;
}
else
{
if ((p->link)->exp >= iy)
p = p->link;
else
connection(ix, iy, p); break;
}
}
if (p->exp < iy)
connection(ix, iy, head);

}

}
//加法算法试下来也没有
polynode polynode::operator+(polynode &p)
{
int m, n;
static polynode result;
poly b, q;
m = number;
n = p.number;
b = head->link;
q = (p.head)->link;
while (m != 0 && n != 0)
{
if (b->exp == q->exp)
{
result.attach(b->coe + q->coe, b->exp);
b = b->link;
q = q->link;
m--; n--;
}
else {
if (b->exp > q->exp)
{
result.attach(b->coe, b->exp);
b = b->link;
m--;
}
else {

                result.attach( q->coe, q->exp);
                    q = q->link;
                n--;
            }
    }
}
if (m == 0 && n == 0)
{
    result.printpoly();
    return result;
}
else {
    if (m == 0)
    {
        while (n--)
        {
            result.attach(q->coe, q->exp);
            q = q->link;
        }
    }
    else
        while (m--)
        {
            result.attach(b->coe, b->exp);
            b = b->link;
        }
    result.printpoly();
    return result;
}

}
polynode& polynode::operator=(const polynode& p)
{

int k = p.number;
poly temp;
temp = (p.head)->link;

    while (k--)
    {

        rear->link = new polynomial;
        rear->link->coe = temp->coe;
        rear->link->exp = temp->exp;
        rear = rear->link;
        temp = temp->link;
    }

return *this;

}
void polynode::printpoly()
{
cout << "print:" << endl;
poly p=head->link;
int m;
m = number;
while (m--)
{
cout << p->coe << p->exp << " ";
p = p->link;
}
cout << endl;
}
polynode::~polynode()
{
poly p=head;

while (number--) {
    p = head;
    head = head->link;
    delete p;
}

}

int main()
{
polynode p1,p2,p3;
p1.readpoly();
p1.printpoly();
p2.readpoly();
p3=p1 + p2;
p3.printpoly();
system("pause");
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-10-17 02:35
    关注

    polynode::polynode(const polynode &p)
    {
    int k = p.number;
    poly temp,head,rear;
    temp= (p.head)->link;
    head = new polynomial;
    head->link = NULL;
    rear = head;
    while (k--)
    {

        rear->link = new polynomial;
        rear->link->coe = temp->coe;
        rear->link->exp = temp->exp;
        rear = rear->link;
        temp = temp->link;
    }
    

    }
    这里不对,poly temp,head,rear;你又定义了head和rear,所以操作的不是成员变量的head和rear,而是同名的局部变量。

    导致temp = (p.head)->link;
    这里head没有初始化

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题