C++,引发了异常: 读取访问权限冲突。 _Right_data 是 0xCCCCCCCC。

调试时遇到的问题;

原代码

#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);
	}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值