I just started programming in C++, and I've tried to create 2 classes where one will contain the other.
File A.h
:
#ifndef _A_h
#define _A_h
class A{
public:
A(int id);
private:
int _id;
B _b; // HERE I GET A COMPILATION ERROR: B does not name a type
};
#endif
File A.cpp
:
#include "A.h"
#include "B.h"
#include <cstdio>
A::A(int id): _id(id), _b(){
printf("hello\n the id is: %d\n", _id);
}
File B.h
:
#ifndef _B_h
#define _B_h
class B{
public:
B();
};
#endif
File B.cpp
:
#include "B.h"
#include <cstdio>
B::B(){
printf("this is hello from B\n");
}
I first compile the B class and then the A class, but then I get the error message:
A.h:9: error: ‘B’ does not name a type
How do I fix this problem?
What happens if a class does not have a name? A class without a name will not have a destructor. The object is made so constructor is required but the destructor is not.
Note that you can also get this error if you place an extern reference to a declaration in a . h / . hpp file before the class is defined, even when you have the actual declaration after the . h / .
This is a very frustrating error message in Qt Creator: 'XYZ' does not name a type . This usually means that there is an error in the class XYZ that prevents the compiler from generating the type, but there are no additional hints as to what went wrong.
Yes, we can create a class without a name using the Anonymous class. Anonymous class is an inner class which does not have a name and whose instance is created at the time of the creation of class itself and these classes are somewhat different from normal classes in its creation.
The preprocessor inserts the contents of the files A.h
and B.h
exactly where the include
statement occurs (this is really just copy/paste). When the compiler then parses A.cpp
, it finds the declaration of class A
before it knows about class B
. This causes the error you see. There are two ways to solve this:
B.h
in A.h
. It is generally a good idea to include header files in the files where they are needed. If you rely on indirect inclusion though another header, or a special order of includes in the compilation unit (cpp-file), this will only confuse you and others as the project gets bigger.If you use member variable of type B
in class A
, the compiler needs to know the exact and complete declaration of B
, because it needs to create the memory-layout for A
. If, on the other hand, you were using a pointer or reference to B
, then a forward declaration would suffice, because the memory the compiler needs to reserve for a pointer or reference is independent of the class definition. This would look like this:
class B; // forward declaration class A { public: A(int id); private: int _id; B & _b; };
This is very useful to avoid circular dependencies among headers.
I hope this helps.
error 'Class' does not name a type
Just in case someone does the same idiotic thing I did ... I was creating a small test program from scratch and I typed Class instead of class (with a small C). I didn't take any notice of the quotes in the error message and spent a little too long not understanding my problem.
My search for a solution brought me here so I guess the same could happen to someone else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With