| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 720 人关注过本帖
标题:[求助]我初学c++,写一个求稀疏多项式的程序,可是错误让我不知所措!!
只看楼主 加入收藏
yfgo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-5-27
收藏
 问题点数:0 回复次数:2 
[求助]我初学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 时出错.
搜索更多相关主题的帖子: 多项式 Lnode 不知所措 coef 
2007-05-27 16:24
leilinghua
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2006-10-25
收藏
得分:0 
类跟结构体不同,默认情况下成员访问属性是private的,显示声明public就可以了

潜心学习。。。。。 Q群:C&&C++ Lovers 39951868
2007-05-27 16:55
yfgo
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-5-27
收藏
得分:0 
太感谢了哈,呵呵!!!!
2007-05-27 17:01
快速回复:[求助]我初学c++,写一个求稀疏多项式的程序,可是错误让我不知所措! ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018146 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved