Thinking in C++ 有问题??!!第17chapter,异常处理程序有问题!有这个头文件吗?

czyf2001 2003-09-26 03:19:40
各位大侠:
我在学习c++编程思想的时候,第17chapter,异常处理总是有一个<except.h>头文件,通过vc6.0在编译的时候总是过不去,对于:except.cpp该例:发现set_unexpected()函数是<eh.h>中定义的,将<except.h>改为<eh.h>并且将for(int i=1; i<=3; i++)改为for(int i=1; i<=2; i++)因为源程序中只有两个抛出类。编译通过!
而在:17.4清除中,我就不知道该如何调试了:源程序如下:

//:CLEANUP.CPP --- Exceptions clear up objects

#include <fstream.h>
#include <except.h> //c++中有这个头文件吗?我在msdn中也找不到哦!
#include <string.h>
#include <eh.h> //for set_unexpected() ,此处seal加上!

ofstream out ("cleanup.out");
class noisy
{
static int i;
int objnum;
enum { sz = 40 };
char name [sz];

public:
noisy(const char *nm = "array elem ") //throw (int)
{
objnum=i++;
memset(name ,0,sz);
strncpy(name, nm, sz -1);
out << "construcing noisy " << objnum
<< " name [ "<< name <<" ] " << endl;
if ( objnum ==5 ) throw int ( 5 );
//Not in exception specification :
if ( *nm == 'z') throw char ( 'z' );

}
~noisy()
{
out << "destructing noisy " << objnum
<< "name [" << name << "] " << endl;

}

void *operator new [] ( size_t sz )
{
out << " noisy::new []" << endl;
return ::new char [sz];
}

void operator delete [] ( void * p)
{
out << "noisy::delete[]" << endl;
::delete []p;
}


};

int noisy::i =0;

void unexpected_rethrow()
{
out << "inside unexpected_rethrow()" << endl;
throw; //Rethrow same exception
}

void main()
{
set_unexpected (unexpected_rethrow);
try
{
noisy nl( "before array" );
// Throws exception:
noisy * array = new noisy [7];
noisy n2("after array");

}
catch (int i)
{
out << "caught" << i << endl;
}

out << "testing unexpected:" << endl;
try
{
noisy n3 ("before unexpected");
noisy n4 ("z");
noisy n5 ("after unexpected");
}
catch (char c)
{
out << "caught" << c << endl;
}
}
此时,编译不能通过!重新将://include <except.h> ,编译通过,但是没有捕获到任何异常啊??好像不对哦!该怎么做呢??!!
...全文
406 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wolf0403 2003-10-03
  • 打赏
  • 举报
回复
没有 .h 的是新的 C++ 头文件
using namespace std; 是引用 C++ std 命名空间。
如果不理解,建议看 C++ Primer Plus 入门。
czyf2001 2003-10-03
  • 打赏
  • 举报
回复
#include <cstring>
#include <stdexcept>
#include <fstream>


这也是定义头文件吗??

using namespace std;这是做什么的啊??!
czyf2001 2003-10-01
  • 打赏
  • 举报
回复
to: Wolf0403(完美废人) 我按照你的帮助还是不行啊?、能再详细点不?
czyf2001 2003-10-01
  • 打赏
  • 举报
回复
能给我解释吗?include后面包括的也是头文件?
在vc71中也不行啊??!
能帮我重新编译看看不??
Wolf0403 2003-09-30
  • 打赏
  • 举报
回复
#include <cstring>
#include <stdexcept>
#include <fstream>
using namespace std;
之后,VC71 编译通过。
czyf2001 2003-09-30
  • 打赏
  • 举报
回复
//:WRAPPED.CPP ---Safe , atomic pointers

#include <fstream.h>
#include <stdlib.h>


typedef unsigned int ss_t;

ofstream out ("wrapped.out");

//simplifed. Yours may have others arguments
template <class T , int sz = 1> class pwrap
{
T *ptr;
public:
class rangeError {}; //Exception class
pwrap ()
{
ptr = new T[sz];
out << "pwrap constructor " <<endl;
}

~pwrap ()
{
delete []ptr ;
out << "pwrap destructor " << endl;
}

T&operator [] (int i) // throw (rangeError)
{
if ( i >= 0 && i <sz)
return ptr [1];
throw rangeError;
}

};


class bonk
{
public:
bonk() { out << "bonk () "<< endl;}
~bonk() { out << "~bonk ()" << endl;}
void g() {}
};

class og()
{
public:
void* operator new [](ss_t sz)
{
out << "allocating an og " << endl;
throw int (47);
return 0;
}

void operator delete [](void *p)
{
out << "deallocating an og" << endl;
::delete p;
}
};

class useResources
{
pwrap<bonk , 3> Bonk;
pwrap<og> Og;

public:
useResources() : Bonk(), Og()
{
out << " useResources() " << endl;
}

~useResources()
{
out << " ~useResources() " << endl;
}

void f() { Bonk [1].g() ; }

};

main ()
{
try
{
useResources ur;
}
catch ( int )
{
out << "inside handler " << endl;
}
catch ( ... )
{
out << "inside catch( ... ) " << endl;
}
return 0;
}



这是一个采用模板来使得分配资源的对象也被正确的清除!在c++编程思想373页。
但是,无论我怎么调程序都出错:vc6.0下编译:

--------------------Configuration: wrapped - Win32 Debug--------------------
Compiling...
wrapped.cpp
E:\study\study c++\program\c++thinking\chapter17\wrapped\wrapped.cpp(49) : error C2143: syntax error : missing ';' before 'public'
E:\study\study c++\program\c++thinking\chapter17\wrapped\wrapped.cpp(51) : error C2143: syntax error : missing ';' before '{'
E:\study\study c++\program\c++thinking\chapter17\wrapped\wrapped.cpp(58) : error C2601: 'delete[]' : local function definitions are illegal
E:\study\study c++\program\c++thinking\chapter17\wrapped\wrapped.cpp(67) : error C2923: 'pwrap' : 'og' is invalid as template argument '#1', type expected
E:\study\study c++\program\c++thinking\chapter17\wrapped\wrapped.cpp(47) : see declaration of 'og'
E:\study\study c++\program\c++thinking\chapter17\wrapped\wrapped.cpp(67) : error C2079: 'Og' uses undefined class 'pwrap<int,1>'
Error executing cl.exe.

wrapped.obj - 5 error(s), 0 warning(s)


“E:\study\study c++\program\c++thinking\chapter17\wrapped\wrapped.cpp(58) : error C2601: 'delete[]' : local function definitions are illegal” delete的定义明明是正确的啊??!
我该怎么做啊??
谢谢各位大侠!
















czyf2001 2003-09-29
  • 打赏
  • 举报
回复
请问:
开始调用:set_unexpected (unexpected_rethrow);
应该在输出中包含:inside unexpected_rethrow()
可是最后结果里面却没有?!
为什么啊??!
set_unexpected (unexpected_rethrow);有什么作用啊??!
谢谢!
bailingke 2003-09-27
  • 打赏
  • 举报
回复
那本书的例程最好在DEVC++下编译
czyf2001 2003-09-27
  • 打赏
  • 举报
回复
好像上面的也不对哦!
我根据set_unexpected()函数,我在c++高级参考手册中找到它是属于<eh.h>的,事实上加上<eh.h>后也是能够在vc中编译过的。
但是我还想问问:如何在Tru64unix下编译通过啊??!
WuYL7812 2003-09-26
  • 打赏
  • 举报
回复
用 stdexcept 试一下
#include <stdexcept>
pasband 2003-09-26
  • 打赏
  • 举报
回复
这个except.h是Borland定义的,所以在borland的产品里都有(比如经典的BC++3.1或c++ builder),vc里有个excpt.h吧,不知道是不是类似的作用。大师们举例子的时候不喜欢提及微软的东东吧,hoho
Wolf0403 2003-09-26
  • 打赏
  • 举报
回复
是不是 include <exception>, using namespace std?
mfcer2 2003-09-26
  • 打赏
  • 举报
回复
C++ Builder 5。0 试用版中有的。还有源程序except.c哦!
看看吧!
czyf2001 2003-09-26
  • 打赏
  • 举报
回复
其实是在cleanup.out中输出了!!

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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