上面一段代码看不出有什么有关问题,但是总是提示异常
下面一段代码看不出有什么问题,但是总是提示错误?
#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编译正常
#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编译正常