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

有关类指针的问题!!

[复制链接]
发表于 2009-11-6 01:15:06 | 显示全部楼层 |阅读模式 IP:江苏扬州
下面的程序我知道不规范,我只是想形象说明一下我的问题.请大家重点回答我的问题,谢!

#include<iostream.h>
#include<stdlib.h>
#include<string.h>
class stu{
public :
int name ;
stu *next;
stu()
{
name=100;
next=NULL;
}
};

void main()
{
stu *top,t;

top=t;

top->name=8;//在指针里我输入一个数

cout<<top-name<<endl;//我以为会输出8!

}

请问我想在一个类的指针下输入一个数,应该怎样做>??
发表于 2009-11-6 01:15:07 | 显示全部楼层 IP:江苏扬州
lz,是输出8啊~~~~~
先改正一下你程序里的错误~~~
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
class stu
{
public :
int name ;
stu *next;
stu()
{
name=100;
next=NULL;
}
};

void main()
{
stu *top,t;

top=&t; //top为指针,而t为对象,应该要使指针top指向对象t;

top->name=8;//在指针里我输入一个数

cout<<top->name<<endl;//我以为会输出8!

}
截不了图给你看
但在我的机器上运行是输出8的~~~
回复

使用道具 举报

发表于 2009-11-6 01:15:08 | 显示全部楼层 IP:江苏扬州
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
class stu{
public :
int name ;
stu *next;
stu()
{
name=100;
next=NULL;
}
};
stu* ask(stu *top)
{
stu t;
top=&t;
if(!top)
{cout<<"err"<<endl;
exit(0);
}
return top;
}
stu* input(stu *top)
{
stu *old,*star,t;
cout<<star<<endl;
old=ask(old);
star=ask(star);

cout<<star<<endl;
old=top;
int i=0;
while(i<3)
{
cout<<"name:";

cin>>star->name;
old->next =star;
old=star;
i++;

star=ask(star);
}
return top;


}
void print(stu *top)
{
int i=0;
stu *star;
star=top;
for(i=0;i<3;i++)
{
cout<<"name:"<<star->name<<endl;
star=star->next;
}
}
void main()
{
stu *top,t;
top=&t;
input(top);
print(top);
}

可能是我的思路错了.我想用这种方法把输入的数输出!麻烦你帮帮忙!谢!
回复

使用道具 举报

发表于 2009-11-6 01:15:09 | 显示全部楼层 IP:江苏扬州
我想问一下你这段代码有什么用 ?

stu* ask(stu *top)
{
stu t;
top=&t;
if(!top)
{cout<<"err"<<endl;
exit(0);
}
return top;
}
回复

使用道具 举报

发表于 2009-11-6 01:15:11 | 显示全部楼层 IP:江苏扬州
对楼主的代码做了以下修改:
我实在看不懂stu* ask(stu *top)这个函数对整个程序有什么作用
所以只好把它删了

#include<iostream.h>
#include<stdlib.h>
#include<string.h>
class stu
{
public :
int name ;
stu *next;
stu()
{
name=100;
next=NULL;
}
};
stu* input(stu* &top)
{
cout<<"please input name:"<<endl;
stu *p2,*p1;
p2=new stu;
top=0;


for(int i=0;i<3;i++)
{
cin>>p2->name;
if(top==NULL) top=p2;
else p1->next=p2;
p1=p2;
p2=new stu;

}

p1->next=NULL;
delete p2;
return top;
}
void print(stu *top)
{
int i=0;
stu *star;
star=top;
for(i=0;i<3;i++)
{
cout<<"name:"<<star->name<<endl;
star=star->next;
}
}

void main()
{
stu *top;
input(top);
print(top);
}
回复

使用道具 举报

发表于 2009-11-6 01:15:12 | 显示全部楼层 IP:江苏扬州
谢!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-1 17:29 , Processed in 0.190238 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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