帮忙看下这段C++程序有什么问题

idea_yuye 2008-10-28 12:17:18
#include<iostream>
using namespace std;
class A
{
int top;
int buffer[100];
public:
A() {top=-1;}
bool push(int i)
{
if(top==100-1)
{
cout <<"stack is overflow.\n";
return false;
}
else
{
top++;
buffer[top]=i;
return true;
}
}
public:
void disp(int bufferd[])
{
int i;
for(i=0;i<=top;i++)
cout << bufferd[i];
}
}
int main()
{
int x;
A st;
cin >> x;
st.push(x);
st.disp(st.buffer);
return 1;
}
请帮我讲解一下错在那!非常感谢啊!
...全文
76 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
phz1985 2008-10-28
  • 打赏
  • 举报
回复
要细心

#include <iostream>
using namespace std;
class A
{
int top;
int buffer[100];
public:
A() {top=-1;}
bool push(int i)
{
if(top==100-1)
{
cout <<"stack is overflow.\n";
return false;
}
else
{
top++;
buffer[top]=i;
return true;
}
}
public:
void disp()
{
int i;
for(i=0;i <=this->top;i++)
cout << this->buffer[i];//
}
};//
int main()
{
int x;
A st;
cin >> x;
st.push(x);
st.disp();
return 0;//
}
jia_xiaoxin 2008-10-28
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;
class A
{
int top;
int buffer[100];
public:
A() {top=-1;}
bool push(int i)
{
if(top==100-1)
{
cout <<"stack is overflow.\n";
return false;
}
else
{
top++;
buffer[top]=i;
return true;
}
}
public:
void disp(int bufferd[])
{
int i;
for(i=0;i <=top;i++)
cout << bufferd[i];
}
}; //此处未加;
int main()
{
int x;
A st;
cin >> x;
st.push(x);
st.disp(st.buffer); //buffer被定义成私有成员函数,所以在此处不能调用
return 1;
}
xhs_lh04 2008-10-28
  • 打赏
  • 举报
回复
void disp()
{
int i;
for(i=0;i <=top;i++)
cout < < buffer[i];
}

buffer是私有变量,st.buffer不能直接访问
太乙 2008-10-28
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;
class A
{
int top;
int buffer[100];
public:
A() {top=-1;}
bool push(int i)
{
if(top==100-1)
{
cout <<"stack is overflow.\n";
return false;
}
else
{
top++;
buffer[top]=i;
return true;
}
}
public:
void disp()
{
int i;
for(i=0;i <=top;i++)
cout << buffer[i];
}
};
int main()
{
int x;
A st;
cin >> x;
st.push(x);
st.disp();
return 1;
}
机智的呆呆 2008-10-28
  • 打赏
  • 举报
回复
int buffer[100]; 是私有的~~~

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧