找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1927|回复: 9

请教运算符重载的问题

[复制链接]
发表于 2009-11-3 02:45:36 | 显示全部楼层 |阅读模式 IP:江苏扬州
#include<iostream.h>
const int size=100;
class Node
{
public:
int data;
Node *next;
Node operator=(Node n);
Node operator<(Node n);
};
int ListLength(Node *p)
{
int len=0;
while(p->next!=NULL)
{
len++;
p=p->next;
}
return len;
}
Node* LocList(Node *p,int data)
{
Node *q;
q=p;
int j=0;
while(j<data)
{
q=q->next;
j++;
}
return (q);
}
int Node::operator= (Node n)
{
}
void Insert(Node number,Node *opt)
{
Node *q=new Node;
Node *cp,*ap,*t;
cp=NULL;
t=ap=opt;
q->data=number;
if(ap==NULL)
{
ap->next=q;
q->next=NULL;
}
else
while(cp!=NULL)
if(number<cp->data)break;
else
{
ap=cp;//ap永远指向的是cp的前个地址
cp=cp->next;
}
number->next=cp;
ap->next=number;
while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
}
void main()
{
/*Node x,y,z;//静态建立三个节点
Node *p;
p=&x;
cout<<"输入x的data:";
cin>>x.data;
cout<<"输入y的data:";
cin>>y.data;
cout<<"输入z的data:";
cin>>z.data;
x.next=&y;
y.next=&z;
z.next=NULL;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;*/
int n;
Node *p,*q,*l,*o;//动态建立三个节点
Node node;
o=p=l=new Node;
for(int i=0;i<10;i++)
{
q=new Node;
cout<<"请输入data:";
cin>>q->data;
p->next=q;
p=q;//此时p指针指向q
}
p->next=NULL;
cout<<"当前链表的长度是:"<<ListLength(l)<<endl;
cout<<"请输入定位的数值:";
cin>>n;
cout<<LocList(l,n)<<endl;
p=l->next;//p是第一个元素节点,l是头节点
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
Insert(node,o);
}
我用了很多方法,但是编译都说是错的,只好请教大家.在这个程序里,运算符怎么重载啊?
发表于 2009-11-3 02:45:37 | 显示全部楼层 IP:江苏扬州
不只是这个问题,还有几处其他问题,你把全部程序再看一遍
回复

使用道具 举报

发表于 2009-11-3 02:45:38 | 显示全部楼层 IP:江苏扬州
我试了,我把Insert函数给删除了,程序可以运行
回复

使用道具 举报

发表于 2009-11-3 02:45:39 | 显示全部楼层 IP:江苏扬州
你创建的又不是有序链表,为什么要用&lt;比较后插入呢?
回复

使用道具 举报

发表于 2009-11-3 02:45:40 | 显示全部楼层 IP:江苏扬州
你这里面对于重载的思想就有问题,首先,你想让int类型通过重载=运算符可以等于累Node中的data成员,但是这种思想是错误的,如果Node类型的成员变量有几个int,几个char,几个string类型的话你是不是都把这几种类型的运算符重载一下,重载的思想是针对于对象级别的,q->data是数据,你让q->data = number(对象级别),这在思想上就有问题,而且由于运算符左边是int类型,所以需要重载全局操作符,但是=并不支持被静态重载或者非成员重载,所以并不可能重载我把你的代码修改了一下,你看看符不符合你本来的想法:
  1. #include<iostream.h>
  2. const int size=100;
  3. class Node
  4. {
  5. public:
  6. int data;
  7. Node *next;
  8. Node& operator=(Node& n);
  9. bool operator<(Node& n);
  10. };
  11. int ListLength(Node *p)
  12. {
  13. int len=0;
  14. while(p->next!=NULL)
  15. {
  16. len++;
  17. p=p->next;
  18. }
  19. return len;
  20. }
  21. Node* LocList(Node *p,int data)
  22. {
  23. Node *q;
  24. q=p;
  25. int j=0;
  26. while(j<data)
  27. {
  28. q=q->next;
  29. j++;
  30. }
  31. return (q);
  32. }
  33. Node& Node::operator= (Node& n)
  34. {
  35. //将Node中除了next指针外的数据成员全部进行拷贝
  36. return *this;
  37. }
  38. bool Node::operator<(Node& n)
  39. {
  40. return false; //比较Node类型中的大小关键字
  41. }
  42. void Insert(Node number,Node *opt)
  43. {
  44. Node *q=new Node;
  45. Node *cp,*ap,*t;
  46. cp=NULL;
  47. t=ap=opt;
  48. *q=number; //对象级别的拷贝,拷贝非next指针外的所有数据
  49. if(ap==NULL)
  50. {
  51. ap->next=q;
  52. q->next=NULL;
  53. }
  54. else
  55. while(cp!=NULL)
  56. if(number < *cp)break;
  57. else
  58. {
  59. ap=cp;//ap永远指向的是cp的前个地址
  60. cp=cp->next;
  61. }
  62. number=*cp;
  63. ap->next=number.next;
  64. while(t!=NULL)
  65. {
  66. cout<<t->data<<" ";
  67. t=t->next;
  68. }
  69. }
  70. void main()
  71. {
  72. int n;
  73. Node *p,*q,*l,*o;//动态建立三个节点
  74. Node node;
  75. o=p=l=new Node;
  76. for(int i=0;i<10;i++)
  77. {
  78. q=new Node;
  79. cout<<"请输入data:";
  80. cin>>q->data;
  81. p->next=q;
  82. p=q;//此时p指针指向q
  83. }
  84. p->next=NULL;
  85. cout<<"当前链表的长度是:"<<ListLength(l)<<endl;
  86. cout<<"请输入定位的数值:";
  87. cin>>n;
  88. cout<<LocList(l,n)<<endl;
  89. p=l->next;//p是第一个元素节点,l是头节点
  90. while(p!=NULL)
  91. {
  92. cout<<p->data<<" ";
  93. p=p->next;
  94. }
  95. cout<<endl;
  96. Insert(node,o);
  97. }
复制代码
回复

使用道具 举报

发表于 2009-11-3 02:45:42 | 显示全部楼层 IP:江苏扬州
Node& operator=(Node& n);
这句话是什么意思呢?为什么Node&要加上引用符号呢?
回复

使用道具 举报

发表于 2009-11-3 02:45:44 | 显示全部楼层 IP:江苏扬州
一为效率,二是因为你本身就在重载=进行对象的拷贝,你怎么可能再在参数传递过程中对Node类型的对象进行拷贝
回复

使用道具 举报

发表于 2009-11-3 02:45:52 | 显示全部楼层 IP:江苏扬州
也就是说,此时的引用是用来进行对象拷贝的吧
回复

使用道具 举报

发表于 2009-11-3 02:45:56 | 显示全部楼层 IP:江苏扬州
no,引用其实和指针的效果差不多,lz要把基本功加强下
回复

使用道具 举报

发表于 2009-11-3 02:45:58 | 显示全部楼层 IP:江苏扬州
  1. #include<iostream>
  2. using namespace std;

  3. const int size=100;

  4. class Node
  5. {
  6. private:
  7. int data;
  8. Node *next;

  9. public:
  10. Node(){ data = 0; next = NULL;}
  11. Node(int d, Node * n)
  12. {
  13. data = d;
  14. next = n;
  15. }
  16. Node(Node & n);
  17. void setData(int d){ data = d;}
  18. void setNext(Node * n)
  19. {
  20. next = n;
  21. }
  22. int getData(){return data;}
  23. Node * getNext(){return next;}
  24. bool operator==(const Node & n);
  25. Node & operator=(const Node & n);
  26. bool operator<(const Node & n);
  27. };


  28. Node::Node(Node & n) // this is not deep copy
  29. {
  30. data = n.data;
  31. next = n.next;
  32. }

  33. bool Node::operator ==(const Node & n)
  34. {
  35. return (data == n.data && next == n.next);
  36. }

  37. Node & Node::operator= (const Node & n) // this is not deep copy
  38. {
  39. this->data = n.data;
  40. this->next = n.next;
  41. return *this;
  42. }

  43. bool Node::operator <(const Node & n)
  44. {
  45. return (data < n.data);
  46. }

  47. int ListLength(Node * p)
  48. {
  49. Node * temp = p;
  50. int len=0;
  51. while(temp->getNext()!=NULL)
  52. {
  53. len++;
  54. temp = temp->getNext();
  55. }
  56. return len;
  57. }



  58. int main()
  59. {
  60. Node x,y,z;

  61. int data;
  62. cout<<"input the data of Node x:";
  63. cin>>data;
  64. x.setData(data);

  65. cout<<"input the data of Node y:";
  66. cin>>data;
  67. y.setData(data);

  68. cout<<"input the data of Node z:";
  69. cin>>data;
  70. z.setData(data);

  71. x.setNext(&y);
  72. y.setNext(&z);
  73. z.setNext(NULL);

  74. Node p = x;

  75. Node *pp = &p;
  76. while(pp!=NULL)
  77. {
  78. cout<<pp->getData()<<" ";
  79. pp =pp->getNext();
  80. }
  81. cout<<endl;

  82. return 0;
  83. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 11:26 , Processed in 0.267633 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表