2

I've write a experimental library and on working on Array I've got error: unknown class name 'Exception'; did you mean 'std::exception'. How can I fix it?

Array.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

Exception.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_

Output:

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
drmgc
  • 171
  • 3
  • 9
  • 2
    Is that all there is in the header? Could it be that you copy pasted something from the headers, so the header guards on top of `Exception.hpp` are the same as the header guards on another header file, effectively preventing the contents of `Execption.hpp` to be read by the compiler? – Shahbaz Oct 09 '13 at 13:03
  • 1
    It's a case of circular includes. – john Oct 09 '13 at 13:04
  • @john, that's absolutely right, I just noticed the same! – Shahbaz Oct 09 '13 at 13:04
  • This probably isn't the problem, but names that begin with an underscore followed by a capital letter (`_RFwC__ARRAY_HPP_`) and names that contain two consecutive underscores (`__msg`) are reserved to the implementation. Don't use them. – Pete Becker Oct 09 '13 at 14:04

3 Answers3

3

You have a circular loop in your header files. "Exception.cpp" includes "Exception.hpp" which includes "IThrowable.hpp" which includes "String.hpp" which includes "Exception.hpp" again.

This causes the include guards to prevent the second inclusion of Exception.hpp, but then when you use Exception in String.hpp, it's not defined.

There are two common solutions:

  1. Use a forward declaration of Exception in String.hpp, and do not #include it there. This will allow you to use the type in your String.hpp header file, with less of an issue. You would then put the #include into the String.cpp
  2. Rearrange the header files so that you don't have a circular dependency. This isn't always possible, but usually involves taking the common code and placing it elsewhere.

Of the two, #1 is the preferred solution, as it will also reduce dependencies between your headers, which is generally a good thing.

Dave S
  • 20,507
  • 3
  • 48
  • 68
0

Exception.hpp includes String.hpp, which tries to re-include Exception.hpp; circular dependencies like this aren't possible.

Since Exception contains a String, the first inclusion is necessary. So you'll need to change String.hpp so it doesn't include Exception.hpp. If the type is needed, then forward-declare class Exception;, and move any code that needs the full definition into the source file.

Also, don't use reserved names like __msg. I won't comment on the wisdom of trying to replace the standard library with something that looks like Java; I'll never need to deal with that insanity, so it's not my problem.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

There's a circular dependency among the includes.

A forward declaration of Exception will resolve the problem.

A bit of unsolicited advice: an unqualified type called Exception is asking for collisions/confusion. Please either make the name more specific or use a namespace to qualify this very global-sounding name.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88