[求助]我初学c++,写一个求稀疏多项式的程序,可是错误让我不知所措!!
#include<iostream>using namespace std;
class Lnode
{
int exp;//多项式指数
float coef;//多项式系数
Lnode *next;
};
Lnode *creatPloy(int n)
{
Lnode *p,*q,*head;
p=new Lnode;
p->exp=0;p->coef=0;p->next=0;
head=p;
for(int i=1;i<=n;i++)
{
q=new Lnode;
cout<<"请输入第i项的系数:";
cin>>q->coef;
cout<<endl;
cout<<"请输入第i项的指数:";
cin>>q->coef;
q->next=0;
p->next=q;
p=q;
delete q;
}
return head;
}
Lnode *sortPloy(Lnode *head)
{
Lnode *curr,*pre,*p,*q;
p=head->next;
head->next=0;
while(p!=0)
{
curr=head->next;
pre=head;
while(curr!=0&&curr->exp<=p->exp)
{
pre=curr;
curr=curr->next;
}
q=p;
p=p->next;
q->next=pre->next;
pre->next=q;
}
return head;
}
Lnode *addPloy(Lnode *head)//多项式的加法
{
Lnode *p;
p=head;
while(p->next!=0)
{
if(p->exp==p->next->exp)
{
p->coef=p->coef+p->next->coef;
p->next=p->next->next;
p=p->next;
}
else
{
p=p->next;
}
}
return head;
}
Lnode *subPloy(Lnode *head)//多项式的减法
{
Lnode *p;
p=head;
while(p->next!=0)
{
if(p->exp==p->next->exp)
{
p->coef=p->coef-p->next->coef;
p->next=p->next->next;
p=p->next;
}
else
{
p=p->next;
}
}
return head;
}
void printPloy(Lnode *head)//打印多项式
{
Lnode *p;
p=head;
cout<<"[";
while(p!=0)
{
cout<<"("<<p->coef<<","<<p->exp<<")";
}
cout<<"]";
}
int main()
{
int m,n;
Lnode *aPtr,*bPtr;
cout<<"请输入你的第一个多项式a的长度:"<<endl;
cin>>m;
cout<<"请输入你的第一个多项式b的长度:"<<endl;
cin>>n;
aPtr=creatPloy(m);
sortPloy(aPtr);
bPtr=creatPloy(n);
sortPloy(bPtr);
cout<<"您输入的a的多项式是:"<<endl;
printPloy(aPtr);
cout<<"您输入的b的多项式是:"<<endl;
printPloy(bPtr);
return 0;
}
只写了一部分,准备只是输出我输入的多项式,可是,调试过程中都是显示的是
H:\调试文件\5\5.cpp(13) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
H:\调试文件\5\5.cpp(13) : error C2248: 'coef' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(6) : see declaration of 'coef'
H:\调试文件\5\5.cpp(13) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(19) : error C2248: 'coef' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(6) : see declaration of 'coef'
H:\调试文件\5\5.cpp(22) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
H:\调试文件\5\5.cpp(23) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(24) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(33) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(34) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(37) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(39) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
H:\调试文件\5\5.cpp(39) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
H:\调试文件\5\5.cpp(42) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(45) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(46) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(46) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(47) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(59) : error C2248: 'coef' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(6) : see declaration of 'coef'
H:\调试文件\5\5.cpp(59) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
执行 cl.exe 时出错.
#include<iostream>
using namespace std;
class Lnode
{
int exp;//多项式指数
float coef;//多项式系数
Lnode *next;
};
Lnode *creatPloy(int n)
{
Lnode *p,*q,*head;
p=new Lnode;
p->exp=0;p->coef=0;p->next=0;
head=p;
for(int i=1;i<=n;i++)
{
q=new Lnode;
cout<<"请输入第i项的系数:";
cin>>q->coef;
cout<<endl;
cout<<"请输入第i项的指数:";
cin>>q->coef;
q->next=0;
p->next=q;
p=q;
delete q;
}
return head;
}
Lnode *sortPloy(Lnode *head)
{
Lnode *curr,*pre,*p,*q;
p=head->next;
head->next=0;
while(p!=0)
{
curr=head->next;
pre=head;
while(curr!=0&&curr->exp<=p->exp)
{
pre=curr;
curr=curr->next;
}
q=p;
p=p->next;
q->next=pre->next;
pre->next=q;
}
return head;
}
Lnode *addPloy(Lnode *head)//多项式的加法
{
Lnode *p;
p=head;
while(p->next!=0)
{
if(p->exp==p->next->exp)
{
p->coef=p->coef+p->next->coef;
p->next=p->next->next;
p=p->next;
}
else
{
p=p->next;
}
}
return head;
}
Lnode *subPloy(Lnode *head)//多项式的减法
{
Lnode *p;
p=head;
while(p->next!=0)
{
if(p->exp==p->next->exp)
{
p->coef=p->coef-p->next->coef;
p->next=p->next->next;
p=p->next;
}
else
{
p=p->next;
}
}
return head;
}
void printPloy(Lnode *head)//打印多项式
{
Lnode *p;
p=head;
cout<<"[";
while(p!=0)
{
cout<<"("<<p->coef<<","<<p->exp<<")";
}
cout<<"]";
}
int main()
{
int m,n;
Lnode *aPtr,*bPtr;
cout<<"请输入你的第一个多项式a的长度:"<<endl;
cin>>m;
cout<<"请输入你的第一个多项式b的长度:"<<endl;
cin>>n;
aPtr=creatPloy(m);
sortPloy(aPtr);
bPtr=creatPloy(n);
sortPloy(bPtr);
cout<<"您输入的a的多项式是:"<<endl;
printPloy(aPtr);
cout<<"您输入的b的多项式是:"<<endl;
printPloy(bPtr);
return 0;
}
只写了一部分,准备只是输出我输入的多项式,可是,调试过程中都是显示的是
H:\调试文件\5\5.cpp(13) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
H:\调试文件\5\5.cpp(13) : error C2248: 'coef' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(6) : see declaration of 'coef'
H:\调试文件\5\5.cpp(13) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(19) : error C2248: 'coef' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(6) : see declaration of 'coef'
H:\调试文件\5\5.cpp(22) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
H:\调试文件\5\5.cpp(23) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(24) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(33) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(34) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(37) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(39) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
H:\调试文件\5\5.cpp(39) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
H:\调试文件\5\5.cpp(42) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(45) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(46) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(46) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(47) : error C2248: 'next' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(7) : see declaration of 'next'
H:\调试文件\5\5.cpp(59) : error C2248: 'coef' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(6) : see declaration of 'coef'
H:\调试文件\5\5.cpp(59) : error C2248: 'exp' : cannot access private member declared in class 'Lnode'
H:\调试文件\5\5.cpp(5) : see declaration of 'exp'
执行 cl.exe 时出错.