C++运行失败,高手指教

haiyan0106 2007-04-19 05:48:52
运行一个简单的C++程序,编译没有错,运行出错了。说是Debug assertion Failed,也不知道怎么回事,请大家指教一下。
...全文
291 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
060 2007-04-19
  • 打赏
  • 举报
回复
有看出一个,不知是不是错误所在
我认为
Queue<elemType> operator +(elemType e)
应当返回Queue<elemType>&, 引用类型.
如果按照lz的返回Queue<elemType> ,将在这个函数返回时,创建临时对象.
060 2007-04-19
  • 打赏
  • 举报
回复
是一个循环队列?

有一个错误
Queue(int First=0,int Last=0,int CurrentNum=0):_First(First),_Last(Last),_CurrentNum(CurrentNum)
{
_pArray=new elemType[MaxNum];
for ( int ix=0; ix < MaxNum; ++ix )
_pArray[ ix ] = 0;
// 既然使用了模版,这个等号右边就不要使用整型的0,可以使用默认构造函数elemType()
}
taodm 2007-04-19
  • 打赏
  • 举报
回复
太长!自己上调试器单步跟踪,看错在哪行。
yutaooo 2007-04-19
  • 打赏
  • 举报
回复
exit(1); 前的cerr有输出吗? 我觉得这里最可疑.
yutaooo 2007-04-19
  • 打赏
  • 举报
回复
看的我一身汗,也没看明白!

继续...
haiyan0106 2007-04-19
  • 打赏
  • 举报
回复
代码如下,大家指教一下。
#include<iostream>

using namespace std;
template <class elemType> class Queue;
const int MaxNum=10;

template <class elemType>
class Queue {
public:
//¹¹Ô캯Êý
Queue(int First=0,int Last=0,int CurrentNum=0):_First(First),_Last(Last),_CurrentNum(CurrentNum){
_pArray=new elemType[MaxNum];
// ³õʼ»¯ÄÚ´æ
for ( int ix=0; ix < MaxNum; ++ix )
_pArray[ ix ] = 0;
}
~Queue(){delete [] _pArray;}//Îö¹¹º¯Êý
bool Empty() const;//Åж϶ÓÁÐÊÇ·ñΪ¿Õ
bool Full() const; //Åж϶ÓÁÐÊÇ·ñÒÑÂú
Queue<elemType> operator +(elemType e);//Ïò¶ÓÁÐÖмÓÈëÔªËØ
elemType operator-();//´Ó¶ÓÁÐÖÐÌáÈ¡ÔªËØ
int getMaxNum(){return MaxNum;}//·µ»Ø»·ÐζÓÔÊÐí´æ´¢µÄÔªËØ¸öÊý×î´óÖµ
int getCurrentNum(){return _CurrentNum;}//·µ»Øµ±Ç°µÄÓÐÐ§ÔªËØ¸öÊý
void Display() const;//ÏÔʾ¶ÓÁÐÖеÄÖµ
private:

int _CurrentNum;//µ±Ç°µÄÓÐÐ§ÔªËØ¸öÊý
int _First;//¶ÓÊ×ÔªËØ
int _Last;//¶ÓÎ²ÔªËØµÄÏÂÒ»¸öλÖÃ
elemType* _pArray;//ÓÃÓÚ´æ·Å¶ÓÁеÄÊý×é
};
//Åж϶ÓÁÐÊÇ·ñΪ¿Õ
template <class elemType>
bool Queue<elemType>::Empty() const
{
return (_CurrentNum==0);
}
//Åж϶ÓÁÐÊÇ·ñÒÑÂú
template <class elemType>
bool Queue<elemType>::Full() const
{

return ((_Last+1)%MaxNum==_First);
}
//Ïò¶ÓÁÐÖмÓÈëÔªËØ
template <class elemType>
Queue<elemType> Queue<elemType>::operator +(elemType e){
if(Full()){
cout<<"¶ÓÁÐÒÑÂú!"<<endl;
}
else{
_pArray[_Last]=e;
_Last=(_Last+1)%MaxNum;
_CurrentNum++;
}
return *this;
}

//´Ó¶ÓÁÐÖÐÌáÈ¡ÔªËØ
template <class elemType>
elemType Queue<elemType>::operator-(){
if(!Empty()){
elemType e=_pArray[_First];
_First=(_First+1)%MaxNum;
_CurrentNum--;
return e;
}
else{
cerr<<"¶ÓÁÐÒѿգ¡"<<endl;
exit(1);
}
}
// ÏÔʾ¶ÓÁÐÖеÄÔªËØ
template <class elemType>
void Queue<elemType>::Display() const
{
for (int i =_First; i<_Last; i =(i++) % MaxNum)
cout<<_pArray[i]<<" ";
cout<<endl;
}

int main()
{
Queue<int> a;
a+10;
// a+14;
// a+3;
// a+6;
// a+10;
// a+14;
// a+3;
// a+6;
// a+10;
//a+14;
//a+3;
//a+6;
a.Display();
// int s=-a;
// cout<<s<<endl;
// a.Display();
// s=-a;
// cout<<s<<endl;
/// a.Display();
// s=-a;
// cout<<s<<endl;
// a.Display();
// s=-a;
// cout<<s<<endl;
// a.Display();
return 0;
}
VCLIFE 2007-04-19
  • 打赏
  • 举报
回复
难道用的是VS2005?
hellox 2007-04-19
  • 打赏
  • 举报
回复
原因很多。.代码呢?
iu_81 2007-04-19
  • 打赏
  • 举报
回复
指针没有初始化,或指针操作不当
mochen5460 2007-04-19
  • 打赏
  • 举报
回复
一般和内存关,数组越界一类的

65,213

社区成员

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

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