求C++大神解答,下面的程序为什么老编译不通过!?我用的VC6? 30
#include<new>#include<exception>#include<iostream>#include<cstdio>/*异常处理!*/usingnames...
#include<new>
#include<exception>
#include<iostream>
#include<cstdio>
/*异常处理!*/
using namespace std;
class A
{
int*p;
int len;
public:
explicit A(int n)throw()
{
p=new (nothrow)int [n];
len=n;
}
int& operator[](int index)
{
if(index<0)
{
throw NegIndex(index);
}
if(index>=len)
{
throw OutOfRange(index);
}
if(p==NULL)
throw MemoryException();
return p[index];
}
class NegIndex:public exception
{
int i;
public:
NegIndex(int index)throw():i(index)
{
}
const char*what()const throw()
{
static char buf[100];
sprintf(buf,"下biao异常",i);
return buf;
}
MemoryException:public exception
{
public:
const char*what()const throw()//const不修改当前函数的数据!
{
return "内存不足";
}
}
};
int main()
{
A B(10);
} 展开
#include<exception>
#include<iostream>
#include<cstdio>
/*异常处理!*/
using namespace std;
class A
{
int*p;
int len;
public:
explicit A(int n)throw()
{
p=new (nothrow)int [n];
len=n;
}
int& operator[](int index)
{
if(index<0)
{
throw NegIndex(index);
}
if(index>=len)
{
throw OutOfRange(index);
}
if(p==NULL)
throw MemoryException();
return p[index];
}
class NegIndex:public exception
{
int i;
public:
NegIndex(int index)throw():i(index)
{
}
const char*what()const throw()
{
static char buf[100];
sprintf(buf,"下biao异常",i);
return buf;
}
MemoryException:public exception
{
public:
const char*what()const throw()//const不修改当前函数的数据!
{
return "内存不足";
}
}
};
int main()
{
A B(10);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询