Error: Class Has Not Been Declared Despite Header Inclusion, and the Code Compiling Fine Elsewhere

error: Class has not been declared despite header inclusion, and the code compiling fine elsewhere

You seem to be saying that the code you are showing doesn't actually produce the compiler error that you are having a problem with. So we can only guess. Here are some possibilities:

  • You could have forgot to include problemclass.h from the file where you are using ProblemClass.
  • You could have misspelled the name of ProblemClass either in its own header file or in the place where you are using it. This can be hard to spot if it is a capitalization error such as writing Problemclass or problemClass instead of ProblemClass.
  • You could have copy-pasted your inclusion guard #defines from one header file to another and then forgot to change the defined names. Then only the first of those two included header files would take effect.
  • You could have placed ProblemClass in a namespace A, in which case you must refer to ProblemClass as A::ProblemClass if you are referring to it from outside the namespace A.
  • You may be using templates and not expecting two-phase lookup to work the way it does.
  • You could have misspelled the file name in your include. The compiler would not report an error on that if you also have an old version of that file under the misspelled name.
  • You could have made ProblemClass a macro that only gets defined after you include problemclass.h, in which case what you see as ProblemClass gets replaced by something else by the macro preprocessor.
  • You could have defined ProblemClass in a header file other than problemclass.h and then problemclass.h actually defines something else.

C++ make error: object_var has not been declared

The cause of your problem:

You have a header file circular dependency problem.

main.cpp includes BasicRobotFunctions.hpp
which includes Robot.hpp
which includes Block.hpp
which includes BasicRobotFunctions.hpp.

If your header files are properly guarded against multiple inclusion (which it seems that they are), Block.hpp won't see the definitions of BasicRobotFunctions.hpp because it is already in the middle of including it.


How to spot the problem:

The source of this problem is apparent in the compilation error message and in your Block.hpp file.

The compiler is reporting an error in Block.hpp, and it is describing line by line how it got to that file via inclusions. The source to your Block.hpp file makes it clear that it is trying to include BasicRobotFunctions.hpp.


The fix:

In your case, you can modify your method signatures in Block.hpp to use a (perhaps constant) reference to the BasicRobotFunctions type, and then forward declare the type. This allows you to eliminate the dependency on the BasicRobotFunctions.hpp header file. (Block.cpp would likely need to include both Block.hpp and BasicRobotFunctions.hpp.)

//...
#include "typedefinitions.h"
#include "dynamixel.h"

class BasicRobotFunctions; // Forward declaration

//...
Block(const BasicRobotFunctions &basic);
//...
int lookAround(const BasicRobotFunctions &basic);
//...

You may be able to avoid this problem in the future by minimizing what headers are required to allow your header file to compile. This means your header file should:

  • Use forward declarations to types that are used.
  • Use references to forward declared types.

You can check that your header file has minimized its dependencies by making sure it compiles by itself. I accomplish this by including the header file first in a corresponding source file, and then make sure the source file compiles.

// Foo.cpp
#include "Foo.hpp"
//...

Compiler keeps telling on 'object' has not been declared...?

With a simple

#include "obj1.h"

in obj2.cpp.

After this, the following will generate an error:

int z = OBJ1::getterX();

because getterX is not static. Either make it static, or call it on an instance of the class.

Bottom line: learn C++ from a book.

Compiling a class with separate source and header

If the header is not in the same directory you must either specify the path in the include command, or you must add -I Path directive to your makefile or include settings.

Maybe this link also helps as codeblock seems to have problems.

http://www.jusuchyne.com/codingchyne/2011/03/codeblocks-failed-to-find-the-header-file/

Namespace has not been declared

Using #include <some_header.h> causes the compiler to search the system include directories before any user directories. Many *nix systems have a system header called select.h already, so you are probably including that instead of your own select.h.

Change all occurrences of:

#include <select.h>

to:

#include "select.h"

Ditto for #include <HW.h>.

Ideally you should not use system header names for your own headers, and you should always use "" for user headers and <> for system headers.

For future reference, a useful technique for debugging such problems is to use g++ -E ... or equivalent to see what headers are actually being included.

Even the simplest class doesn't compile, has not been declared

Its because

 #ifdef A_H

needs to be

 #ifndef A_H

notice the "n", as in if NOT defined.

The former will only compile the code if A_H is defined, which it isn't since you only define it on the next line.



Related Topics



Leave a reply



Submit