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 usingProblemClass
. - 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 writingProblemclass
orproblemClass
instead ofProblemClass
. - 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 namespaceA
, in which case you must refer toProblemClass
asA::ProblemClass
if you are referring to it from outside the namespaceA
. - 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 includeproblemclass.h
, in which case what you see asProblemClass
gets replaced by something else by the macro preprocessor. - You could have defined
ProblemClass
in a header file other thanproblemclass.h
and thenproblemclass.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
Utilizing C++ in iOS and MAC Os X Applications
C++ Trying to Swap Values in a Vector
Why Can't We Declare a Namespace Within a Class
Problems Using Member Function as Custom Deleter with Std::Shared_Ptr
C++ Memory Barriers for Atomics
Do Static Members of a Class Occupy Memory If No Object of That Class Is Created
Implicit Conversion When Overloading Operators for Template Classes
Returning Non-Const Reference from a Const Member Function
Cin.Getline() Is Skipping an Input in C++
Switch "Transfer of Control Bypasses Initialization Of:" When Calling a Function
Using Std::Move() When Returning a Value from a Function to Avoid to Copy
Why Is There No 2-Byte Float and Does an Implementation Already Exist
Performance Difference Between C and C++ Style File Io
Do Pthread Mutexes Work Across Threads If in Shared Memory
How to Parallelize a for Loop Through a C++ Std::List Using Openmp