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

用链表类完成一元多项式的加法 为什么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条回答 默认 最新

  • blownewbee 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没有初始化

    点赞 打赏 评论

相关推荐 更多相似问题