读书人

前置声明和错误规范说明是否可以同时使

发布时间: 2012-02-10 21:27:41 作者: rapoo

【求助】前置声明和异常规范说明是否可以同时使用
在一个类base中,定义一个对外隐藏的异常类baseErr。
并且在可能抛出异常的方法badhappen的声明中说明了异常规范。

class base{
private:
class err; //just declaration , handle class
void badhappen() throw (err);
public:
friend void ex1(); // for access of base::err
};

程序在Dev-C++4.9.9.2下编译报错了:

invalid use of undefined type `struct baseErr'

请问各位大侠 前置声明和异常规范说明是否可以同时使用,如果可以,如何实现 ?

谢谢了!




[解决办法]
当然不需要。只需要class的定义

class base{
private:

class err //definition of class err
{
public:
const char* what() throw(); //declaration
};

void badhappen() throw (err);
public:
friend void ex1(); // for access of base::err
};

const char* base::err::what() throw() //a definition of err's memeber
{
return "hello, world";
}
[解决办法]
总算找到了。。。。见ISO-IEC14882-2003,15.4节:
15.4 Exception specifications [except.spec]
1 A function declaration lists exceptions that its function might directly or indirectly throw by using an exception-specification as a suffix of its declarator.
exception-specification:
throw ( type-id-listopt )
type-id-list:
type-id
type-id-list , type-id
An exception-specification shall appear only on a function declarator in a function, pointer, reference or pointer to member declaration or definition. An exception-specification shall not appear in a typedef declaration.
[Example:
void f() throw(int); // OK
void (*fp)() throw (int); // OK
void g(void pfa() throw(int)); // OK
typedef int (*pf)() throw(int); // ill-formed
—end example] A type denoted in an exception-specification shall not denote an incomplete type(开到了吧:exception-specification中指定的type不可以是incomplete type). A type denoted in an exception-specification shall not denote a pointer or reference to an incomplete type, other than void*, const void*, volatile void*, or const volatile void*.

读书人网 >C++

热点推荐