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
The problem is that you need to include B.h
in your A.h
file. The problem is that in the definition of A
, the compiler still doesn't know what B
is. You should include all the definitions of all the types you are using.
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.
NOTE: Because people searching with the same keyword will land on this page, I am adding this answer which is not the cause for this compiler error in the above mentioned case.
I was facing this error when I had an enum
declared in some file which had one of the elements having the same symbol as my class name.
e.g. if I declare an enum = {A, B, C}
in some file which is included in another file where I declare an object of class A
.
This was throwing the same compiler error message mentioning that Class A does not name a type
. There was no circular dependency in my case.
So, be careful while naming classes and declaring enums (which might be visible, imported and used externally in other files) in C++.
It actually happend to me because I mistakenly named the source file "something.c" instead of "something.cpp". I hope this helps someone who has the same error.
Include "B.h" in "A.h". That brings in the declaration of 'B' for the compiler while compiling 'A'.
The first bullet holds in the case of OP.
$3.4.1/7 -
"A name used in the definition of a class X outside of a member function body or nested class definition27) shall be declared in one of the following ways:
— before its use in class X or be a member of a base class of X (10.2), or
— if X is a nested class of class Y (9.7), before the definition of X in Y, or shall be a member of a base class of Y (this lookup applies in turn to Y’s enclosing classes, starting with the innermost enclosing class),28) or
— if X is a local class (9.8) or is a nested class of a local class, before the definition of class X in a block enclosing the definition of class X, or
— if X is a member of namespace N, or is a nested class of a class that is a member of N, or is a local class or a nested class within a local class of a function that is a member of N, before the definition of class X in namespace N or in one of N’s enclosing namespaces."
You must first include B.h
from A.h
. B b
; makes no sense until you have included B.h
.