我有一个名为“异常”的命名空间的问题
让我们考虑以下示例标题:
#include <exception>
namespace exception
{
struct MyException : public std::exception
{};
}
struct AnotherException : public exception::MyException
{
AnotherException() : exception::MyException() { }
};
此 header 无法编译并出现以下错误:
namespacetest.hpp: In constructor 'AnotherException::AnotherException()': namespacetest.hpp:12:48: error: expected class-name before '(' token namespacetest.hpp:12:48: error: expected '{' before '(' token
There are two solutions for this:
1) qualify namespace with "::" in line 12
AnotherException() : ::exception::MyException() { }
2) 将命名空间重命名为例如“特殊”
命名空间“异常”导致混淆的原因是什么?我知道有一个类 std::exception。这会造成麻烦吗?
最佳答案
I know that there is a class
std::exception
. Does this cause the trouble?
是的。在std::exception
中,非限定名称exception
是注入(inject)的类名。这是继承的,所以在你的类中,一个不合格的 exception
指的是那个,而不是你的命名空间。
关于c++ - 名为 'exception' 的命名空间导致编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22627965/