stack问题????

printfabcd 2009-05-11 11:02:46
void main(void){
string fileName="";
string operate;//操作
ifstream in;
stack<Car*> park;
cout<<"请输入文件名字"<<endl;
cin>>fileName;

cout<<fileName<<endl;
// Open data file
in.open (fileName.c_str(),ios::in);

if (!in) {
cerr << "文件打开失败" << endl;
return;
}

while (1) {
Car car;
in >> car>>operate;
cout<<operate<<park.size()<<endl;
if(operate=="arrives"){
arrives(park,car);
}else {
cout<<"dddddddddddd"<<park.top()->getLicense()<<" "<<car.getLicense()<<endl;
departure(park,car.getLicense());
}

if (in.eof()){
in.close();//到达文件末尾,关闭文件
break;//跳出while循环
}
}
}
//读入data.txt文件的数据,并执行相关操作
//车的行为是“arrives”时,所做的操作
void arrives(stack<Car*>& park,Car& car){
if(park.size()==5){
outputString("Sorry PORSCHE, the lot is full");
return;
}
park.push(&car);//每次执行该语句时,会将栈中所有的数据替换成相同的数据;*********************问题所在
}
例如代码:
TKG-123 arrives
QWE-839 arrives
UTU-K90 arrives
假设开始时,park里面车有一条数据为TKG-123 arrives
但再插入一条数据为QWE-839 arrives的Car指针时,就将第一条数据也改为QWE-839 arrives了,请问这是怎么回事???
...全文
146 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyond071 2009-05-12
  • 打赏
  • 举报
回复
while循环中代码应该做相应的修改:

Car *car = new Car;
in >> *car>> operate;
cout << operate << park.size() << endl;
if(operate=="arrives"){
arrives(park,*car);
}
else {
cout <<"dddddddddddd" <<park.top()->getLicense() <<" " <<car->getLicense() <<endl;
departure(park,car->getLicense());
}
beyond071 2009-05-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 printfabcd 的回复:]
会不会是因为,我存到栈中的是指针,但改变时,就全都改了,
但是好像,指针应该是不一样的吧,
因为每次都会有
Car car;
[/Quote]
问题就在于此,你使用的Car car;建立的是一个临时变量,你把指向临时变量的指针放入到stack <Car*> park中,将导致现严重错误!
在while循环过程中,每次循环后临时变量car都被释放了,也就是说,存放在park中的Car*实际上已经成为了野指针!
出现楼主说的问题:

//假设开始时,park里面车有一条数据为TKG-123 arrives
//但再插入一条数据为QWE-839 arrives的Car指针时,就将第一条数据也改为QWE-839 arrives了,请问这是怎么回事???

应该是编译器每次生成car这个临时变量时,都放在栈的某个指定位置,也就是说前一次的野指针和当前插入的指针指向的是栈上的同一位置!
要解决这个问题,就不能使用临时变量!应使用在堆上的动态分配:
Car *car = new Car;
printfabcd 2009-05-12
  • 打赏
  • 举报
回复
问题解决了,谢谢大家。
hjjdebug 2009-05-12
  • 打赏
  • 举报
回复
顶顶7楼,但六楼分析就有点问题。

void arrives(stack <Car*>& park,Car& car){
if(park.size()==5){
outputString("Sorry PORSCHE, the lot is full");
return;
}
park.push(&car);//每次执行该语句时,会将栈中所有的数据替换成相同的数据;*********************问题所在
}
你的car 是一个局部变量,每次调用arraives, 都会将car 地址放入堆栈。
结果,你的堆栈里保留了5个同样的car 地址。你可以检查一下你堆栈中保存的地址值。
结果成了狗熊掰棒子,前面的都丢了,就剩最后一个car. 哈哈!
修改方法可以用new, 使每次传下的地址不一样。 7 楼给了代码,但记着不用的时候 delete 掉释放内存。
printfabcd 2009-05-11
  • 打赏
  • 举报
回复
会不会是因为,我存到栈中的是指针,但改变时,就全都改了,
但是好像,指针应该是不一样的吧,
因为每次都会有
Car car;
shexinwei 2009-05-11
  • 打赏
  • 举报
回复
不知道原因,同样期待
printfabcd 2009-05-11
  • 打赏
  • 举报
回复
Car.h
#ifndef JOB_H
#define JOB_H
#include <iostream>
#include <string>
using namespace std;
class Car{

public:
//两个构造函数
Car(){
this->moved_times=0;
}
Car(string license):license_plate(license),moved_times(0){

}
Car(string license,int times=0): license_plate(license),moved_times(times){

}
//获取私有属性值
string getLicense() const{
return this->license_plate;
}
int getMovedTimes() const{
return this->moved_times;
}
void setLicense(string license){
this->license_plate=license;
}
void setMovedTimes(int times){
this->moved_times;
}
//对输入运算符的重载的友元函数的声明
friend istream &operator>>(istream &stream, Car &e);
//对输出运算符的重载的友元函数的声明
friend ostream &operator<<(ostream &stream, Car &e);
protected:
private :
string license_plate;//车牌号
int moved_times;//被移动的次数

};
#endif
Car.cpp
#include <iostream>
#include "car.h"
istream &operator>>(istream &stream, Car &j) {
stream >> j.license_plate;
cout<<j.license_plate<<endl;;
//stream >> j.moved_times;

return stream;
}

//将Car类对象以这钟格式“COOLONE was moved 0 times while it was here”输出
ostream &operator<<(ostream &stream, Car &j) {

stream << j.getLicense() << " was moved "
<< j.getMovedTimes()
<< " times while it was here " << endl;

return stream;
}
liliangbao 2009-05-11
  • 打赏
  • 举报
回复
错了
Car结构不明~ 
建议查一下:
in >> car>>operate; //查一下读取的文件数据是否正确~
liliangbao 2009-05-11
  • 打赏
  • 举报
回复
Car结构不明~
建议查一下:
in >> car>>operate; //查一下读取的文件数据释放正确~

65,210

社区成员

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

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