首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

上面一段代码看不出有什么有关问题,但是总是提示异常

2012-10-18 
下面一段代码看不出有什么问题,但是总是提示错误?#include exception#include stdexceptvoid handle_o

下面一段代码看不出有什么问题,但是总是提示错误?
#include <exception>
#include <stdexcept>

void handle_out_of_memory(const std::bad_alloc&) { }

template <class T> class Handle {
public:
  Handle(T *p = 0) try : ptr(p), use(new size_t(1)) { 
  } catch(const std::bad_alloc &e) { handle_out_of_memory(e); }

  // overloaded operators to support pointer behavior
  T& operator*();
  T* operator->();
  const T& operator*() const;
  const T* operator->() const;

  // copy control: normal pointer aliasing; but last Handle deletes the object
  Handle(const Handle& h): ptr(h.ptr), use(h.use) { ++*use; }
  Handle& operator=(const Handle&);
  ~Handle() { rem_ref(); }
private:
  T* ptr;
  size_t *use;
  void rem_ref() { if (--*use == 0) {delete ptr; delete use;} }
};


错误提示:
 missing ';' before 'try'
 。。。。ms_handle.h(54) : see reference to class template instantiation 'Handle<T>' being compiled

请求高手指点

[解决办法]
编译器问题~VS2005编译正常。
[解决办法]
G++编译也是正常的,楼主用的是什么编译器?
[解决办法]
编译正常,楼主是不是没clean,再Rebuild ?

[解决办法]
估计是编译器的版本低了吧,不支持该语法,可以修改为如下形式
Handle(T *p = 0) {
  try {
 ptr = p;
 use = new size_t(1);
  } catch(const std::bad_alloc &e) { handle_out_of_memory(e); }
}
[解决办法]
try
{
。。。
}
catch (CException* e)
{
。。。
}
[解决办法]
VS2008编译正常

热点排行