调试时遇到的问题;
原代码
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
class car
{
string chepaihao;
double arrive_time;//到达的时间
public:
time_t start, end;
int zi_weizhi;//自己的位置
car(string p);//到达时间赋0
static int weizhi;//总共位置
car* next;
string REchepaihao();//返回车牌号
void getchepai(string p);//赋值车牌号
int left_time();
};
int car::weizhi = 0;
/*
* 链栈采用头插法;
* 且链栈不用带头节点(-*-)深得我心
*/
car* t_top;//另一个链表的头指针;
void reading(car*&);
void push(car*& top1, car* now)
{
if (top1 == NULL)
{
top1 = now;
top1->next = NULL;
}
else
{
now->next = top1;
top1 = now;
}
//cout << now->REchepaihao() << "已进入停车位" << endl;
}
void pop(car*&top)
{
car* p = top;
top = top->next;
delete p;
cout <<top->REchepaihao()<< "您已开出停车场或为他人让位"<<endl;
}
int main()
{
car* top;//主要栈链表的头指针;
reading(top);
for (car* index = top;index;index = index->next)
cout << index->REchepaihao() << endl;
}
car::car(string p)
{
chepaihao = p;
weizhi++;
zi_weizhi = weizhi;
//double cost;
time(&start);
//sleep(1);
//time(&end);
//cost = difftime(end, start);
//printf("%f/n", cost);
arrive_time = 0;
next = NULL;
}
string car::REchepaihao()
{
return chepaihao;
}
void car::getchepai(string p)
{
chepaihao = p;
}
int car::left_time()
{
return 0;
}
void reading(car*&top)
{
string a[2] = { "YOVGYC98","UIRBP09" };
for (int i = 0;i < 2;i++)
{
car* index = new car(a[i]);
push(top, index);//这里出了问题;
}
}
问题出在未初始化头指针
调试时问题截图
加入
top = new car(“YUILKVG78”);
正确代码
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
class car
{
string chepaihao;
double arrive_time;//到达的时间
public:
time_t start, end;
int zi_weizhi;//自己的位置
car(string p);//到达时间赋0
static int weizhi;//总共位置
car* next;
string REchepaihao();//返回车牌号
void getchepai(string p);//赋值车牌号
int left_time();
};
int car::weizhi = 0;
/*
* 链栈采用头插法;
* 且链栈不用带头节点(-*-)深得我心
*/
car* t_top;//另一个链表的头指针;
void reading(car*&);
void push(car*& top1, car* now)
{
if (top1 == NULL)
{
top1 = now;
top1->next = NULL;
}
else
{
now->next = top1;
top1 = now;
}
//cout << now->REchepaihao() << "已进入停车位" << endl;
}
void pop(car*&top)
{
car* p = top;
top = top->next;
delete p;
cout <<top->REchepaihao()<< "您已开出停车场或为他人让位"<<endl;
}
int main()
{
car* top;//主要栈链表的头指针;
reading(top);
for (car* index = top;index;index = index->next)
cout << index->REchepaihao() << endl;
}
car::car(string p)
{
chepaihao = p;
weizhi++;
zi_weizhi = weizhi;
//double cost;
time(&start);
//sleep(1);
//time(&end);
//cost = difftime(end, start);
//printf("%f/n", cost);
arrive_time = 0;
next = NULL;
}
string car::REchepaihao()
{
return chepaihao;
}
void car::getchepai(string p)
{
chepaihao = p;
}
int car::left_time()
{
return 0;
}
void reading(car*&top)
{
top = new car("YUILKVG78");
string a[2] = { "YOVGYC98","UIRBP09" };
for (int i = 0;i < 2;i++)
{
car* index = new car(a[i]);
push(top, index);
}
}