Class name does not name a type in C++

前端 未结 10 1959
情深已故
情深已故 2020-12-08 09:37

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          


        
相关标签:
10条回答
  • 2020-12-08 10:02

    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.

    0 讨论(0)
  • 2020-12-08 10:08
    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.

    0 讨论(0)
  • 2020-12-08 10:09

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

    0 讨论(0)
  • 2020-12-08 10:14

    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.

    0 讨论(0)
  • 2020-12-08 10:16

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

    0 讨论(0)
  • 2020-12-08 10:17

    You must first include B.h from A.h. B b; makes no sense until you have included B.h.

    0 讨论(0)
提交回复
热议问题