【问题标题】:Gcc/clang: error: unknown class name 'Exception'; did you mean 'std::exception' [closed]Gcc/clang:错误:未知类名“异常”;你的意思是'std :: exception' [关闭]
【发布时间】:2013-10-09 13:00:55
【问题描述】:

我已经编写了一个实验库,并且正在研究 Array 我有 error: unknown class name 'Exception'; did you mean 'std::exception'。我该如何解决?

数组.hpp

#ifndef _RFwC__ARRAY_HPP_
#define _RFwC__ARRAY_HPP_

#include "util.hpp"
#include "Object.hpp"
#include "IDefaultType.hpp"
#include "Exception.hpp"

class ArrayException : public Exception {}; // here error
class IndexOutOfRangeException : public ArrayException {};

template<typename T_Value>
class Array : public Object, IDefaultType{
public:
    Array(const intnum_t _length);
    Array();
// next array code and closing for guard symbols

异常.hpp

#ifndef _RFwC__EXCEPTION_HPP_
#define _RFwC__EXCEPTION_HPP_

#include "IThrowable.hpp"
#include "Object.hpp"
#include "String.hpp"

class Exception : public Object, IThrowable {
public:
    Exception(const String _message, const Exception * _pInner);
    Exception(const String _message);
    Exception();

    const String getMsg() const;
    const Exception * getInner() const;

    virtual void onThrow();
protected:
    String __msg;
    const Exception * __pInner;
};

#endif //   _RFwC__EXCEPTION_HPP_

输出:

c++ -DPLATFORM_=linux -DCAPACITY_=32 -std=c++11 -fPIC -o obj/core_Exception.cpp.o -c src_Core/Exception.cpp
In file included from src_Core/Exception.cpp:23:
In file included from src_Core/Exception.hpp:26:
In file included from src_Core/IThrowable.hpp:27:
In file included from src_Core/String.hpp:28:
src_Core/Array.hpp:31:31: error: unknown class name 'Exception'; did you mean 'std::exception'?
class ArrayException : public Exception {};
                              ^~~~~~~~~
                          std::exception
/usr/bin/../lib/gcc/i686-linux-gnu/4.7/../../../../include/c++/4.7/exception:61:9: note: 'std::exception' declared here
  class exception
        ^
1 error generated.
make: *** [core_Exception.cpp.o] Ошибка 1

【问题讨论】:

  • 这就是标题中的全部内容吗?难道是你从头文件中复制粘贴了一些东西,所以Exception.hpp上面的头文件保护与另一个头文件上的头文件保护相同,有效地防止了Execption.hpp的内容被编译器读取?
  • 这是一个循环包含的情况。
  • @john,完全正确,我刚刚注意到了!
  • 这可能不是问题,但是以下划线后跟大写字母 (_RFwC__ARRAY_HPP_) 和包含两个连续下划线 (__msg) 的名称保留给实现.不要使用它们。

标签: c++ linux gcc c++11


【解决方案1】:

您的头文件中有一个循环循环。 “Exception.cpp”包括“Exception.hpp”,其中包括“IThrowable.hpp”,其中“String.hpp”又包括“Exception.hpp”。

这会导致包含守卫阻止 Exception.hpp 的第二次包含,但是当您在 String.hpp 中使用 Exception 时,它没有定义。

有两种常见的解决方案:

  1. 在 String.hpp 中使用 Exception 的前向声明,并且不要在其中 #include 它。这将允许您使用 String.hpp 头文件中的类型,而问题较少。然后,您将 #include 放入 String.cpp
  2. 重新排列头文件,使您没有循环依赖。这并不总是可行的,但通常涉及获取通用代码并将其放在其他地方。

在这两者中,#1 是首选解决方案,因为它还可以减少标题之间的依赖关系,这通常是一件好事。

【讨论】:

  • 不,它包含保护符号,但我已从发布的源中删除它们
  • @DrMGC - 再次仔细阅读答案。尤其是第二句。包含守卫不能解决这个问题。
【解决方案2】:

Exception.hpp 包含String.hpp,它试图重新包含Exception.hpp;像这样的循环依赖是不可能的。

由于Exception 包含String,因此第一个包含是必要的。所以你需要更改String.hpp,所以它不包括Exception.hpp。如果需要该类型,则前向声明class Exception;,并将任何需要完整定义的代码移动到源文件中。

另外,不要像__msg 一样使用reserved names。我不会评论尝试用看起来像 Java 的东西替换标准库的智慧。我永远不需要处理这种精神错乱,所以这不是我的问题。

【讨论】:

    【解决方案3】:

    包含之间存在循环依赖关系。

    Exception 的前向声明将解决问题。

    一些不请自来的建议:一个名为Exception 的非限定类型正在请求冲突/混淆。请使名称更具体或使用namespace 来限定这个听起来非常全球化的名称。

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多