Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2338069
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:08:38

有如下的一个例子,我在vc++6.0上编译连接该程序时都是正确的,但是在结果运行时却显示:Debug Error,abnormal  program  termination (please retry to debug the application),不能得到结果。请问这是什么原因?请指教!

例1  下述c++程序演示了在函数调用运算符重载函数operator()中进行二维int型数组下标越界检查,包括检查表示行、列的两个下标中是否越出左边界或右边界,并分别设置throw点,而异常处理仅显示相应的文字信息。程序中列举了正常情形以及多种二维数组下标异常情形。
#include
#include
using namespace std;
class inttwodarray
{
public:
inttwodarray(int,int);
int getsize1() const { return (size1); }
int getsize2() const { return (size2); }
int &operator()(int,int);
const int &operator()(int,int) const;
void print(const inttwodarray &) const;
private:
int *a;
int size1;
int size2;
};
inttwodarray::inttwodarray(int size1,int size2)
{
int size=size1*size2;
try
{
a=new int[size];
}
catch (bad_alloc)
{
cerr<<"Unable to allocate memory space for inttwodarray."< throw;
}
this->size1=size1;
this->size2=size2;
}
int &inttwodarray::operator()(int size1,int size2)
{
if (size1<0)
throw string("Row size out of left bounds.");
else if (size1>=this->size1)
throw string("Row size out of right bounds.");
if (size2<0)
throw string("Column size out of left bounds.");
else if (size2>=this->size2)
throw string("Column size out of right bounds.");
return (a[size1*this->size2+size2]);
}
const int &inttwodarray::operator()(int size1,int size2) const
{
if (size1<0)
throw string("Row size out of left bounds.(const object)");
else if (size1>=this->size1)
throw string("Row size out of right bounds.(const object)");
if (size2<0)
throw string("Column size out of left bounds.(const object)");
else if (size2>=this->size2)
throw string("Column size out of right bounds.(const object)");
return (a[size1*this->size2+size2]);
}
void inttwodarray::print(const inttwodarray &a) const
{
cout<<"a("< for (int i=0;i {
cout<<"  {";
for (int j=0;j if (j cout< else
cout< if (i cout<<",";
cout< }
cout<<"}"<}
void main()
{
inttwodarray a(3,4);
int i,j;
char yes;
for (i=0;i for (j=0;i a(i,j)=2*i+j;
a.print(a);
cout< do
{
try
{
cout<<"Enter i,j(0<=i<="< <<",0<=j<="< cin>>i>>j;
cout<<"a("< }
catch (string s)
{
cerr< cerr<<"i="< }
cout<<"Continue?(Y or N)";
cin>>yes;
cout< } while (yes=='y'||yes=='Y');
cout<<"*****WELL DONE*****"<}

下面是本应该得出而没有得出的运行结果:
a(3,4)={
{0,1,2,3},
{2,3,4,5},
{4,5,6,7}
}
Enter i,j(0<=i<=2,0<=j<=3,)0 0(回车)
a(0,0)=0
Continue?(Y or N)y(回车)


Enter i,j(0<=i<=2,0<=j<=3,)2 3(回车)
a(2,3)=7
Continue?(Y or N)y(回车)


Enter i,j(0<=i<=2,0<=j<=3,)-2 2(回车)
Row size out of left bounds.
i=-2,j=2
Continue?(Y or N)y(回车)


Enter i,j(0<=i<=2,0<=j<=3,)4 2(回车)
Row size out of right bounds.
i=4,j=2
Continue?(Y or N)y(回车)


Enter i,j(0<=i<=2,0<=j<=3,)2 -2(回车)
Row size out of left bounds.
i=2,j=-2
Continue?(Y or N)y(回车)


Enter i,j(0<=i<=2,0<=j<=3,)2 8(回车)
Row size out of right bounds.
i=2,j=8
Continue?(Y or N)y(回车)


Enter i,j(0<=i<=2,0<=j<=3,)1 1(回车)
a(1,1)=3
Continue?(Y or N)n(回车)


*****WELL DONE*****

--------------------next---------------------

阅读(1254) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~