类名不命名 C++ 中的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3608305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class name does not name a type in C++
提问by or.nomore
I just started programming in C++, and I've tried to create 2 classes where one will contain the other.
我刚开始用 C++ 编程,我尝试创建 2 个类,其中一个包含另一个。
File A.h
:
文件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
:
文件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
:
文件B.h
:
#ifndef _B_h
#define _B_h
class B{
public:
B();
};
#endif
File B.cpp
:
文件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:
我首先编译 B 类,然后编译 A 类,但随后我收到错误消息:
A.h:9: error: ‘B' does not name a type
啊:9:错误:'B' 没有命名类型
How do I fix this problem?
我该如何解决这个问题?
回答by Bj?rn Pollex
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:
预处理器插入文件的内容A.h
以及语句发生的B.h
确切位置include
(这实际上只是复制/粘贴)。当编译器随后解析时A.cpp
,它会A
在知道class之前找到 class 的声明B
。这会导致您看到的错误。有两种方法可以解决这个问题:
- Include
B.h
inA.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 classA
, the compiler needs to know the exact and complete declaration ofB
, because it needs to create the memory-layout forA
. If, on the other hand, you were using a pointer or reference toB
, 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.
- 包括
B.h
在A.h
. 在需要的文件中包含头文件通常是一个好主意。如果您依赖于通过另一个头文件的间接包含,或编译单元(cpp 文件)中包含的特殊顺序,这只会随着项目变大而使您和其他人感到困惑。 如果
B
在 class 中使用类型的成员变量A
,编译器需要知道 的准确和完整的声明B
,因为它需要为A
. 另一方面,如果您使用的是指向 的指针或引用B
,那么前向声明就足够了,因为编译器需要为指针或引用保留的内存与类定义无关。这看起来像这样:class B; // forward declaration class A { public: A(int id); private: int _id; B & _b; };
这对于避免标头之间的循环依赖非常有用。
I hope this helps.
我希望这有帮助。
回答by Ivan
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 Classinstead 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.
以防万一有人做了我做过的同样愚蠢的事情......我正在从头开始创建一个小型测试程序,我输入了Class而不是class(带有一个小 C)。我没有注意到错误消息中的引号,并且花了太长时间不理解我的问题。
My search for a solution brought me here so I guess the same could happen to someone else.
我对解决方案的搜索将我带到了这里,所以我想其他人也可能会遇到同样的情况。
回答by Puppy
You must first include B.h
from A.h
. B b
; makes no sense until you have included B.h
.
您必须首先包括B.h
from A.h
。B b
; 在您包含B.h
.
回答by dyno8426
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.
当我enum
在某个文件中声明了一个与我的类名具有相同符号的元素时,我正面临这个错误。
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
.
例如,如果我enum = {A, B, C}
在某个文件中声明了一个,该文件包含在另一个文件中,我在其中声明了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.
这是抛出相同的编译器错误消息,提到Class A does not name a type
. 在我的情况下没有循环依赖。
So, be careful while naming classes and declaring enums (which might be visible, imported and used externally in other files) in C++.
因此,在 C++ 中命名类和声明枚举(可能在其他文件中可见、导入和外部使用)时要小心。
回答by Amir Rachum
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.
问题是您需要包含B.h
在您的A.h
文件中。问题是在 的定义中A
,编译器仍然不知道是什么B
。您应该包括您正在使用的所有类型的所有定义。
回答by Chubsdad
Include "B.h" in "A.h". That brings in the declaration of 'B' for the compiler while compiling 'A'.
在“啊”中包含“Bh”。这会在编译“A”时为编译器带来“B”的声明。
The first bullet holds in the case of OP.
第一个要点适用于 OP。
$3.4.1/7 -
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."
“在成员函数体或嵌套类定义之外的类 X 的定义中使用的名称应以下列方式之一声明:
—在 X 类中使用之前或成为 X 基类的成员(10.2),或
— 如果 X 是 Y 类 (9.7) 的嵌套类,在 Y 中 X 的定义之前,或者应该是 Y 的基类的成员(此查找依次适用于 Y 的封闭类,从最内部的封闭类开始),28) 或
— 如果 X 是本地类 (9.8) 或本地类的嵌套类,则在包含类 X 定义的块中的类 X 定义之前,或
— 如果 X 是命名空间 N 的成员,或者是作为 N 成员的类的嵌套类,或者是作为 N 成员的函数的局部类中的局部类或嵌套类,则在命名空间 N 或 N 的封闭命名空间之一中的类 X 的定义。”
回答by Stephane Rolland
when you define the class A, in A.h, you explicitely say that the class has a member B.
当您定义类 A 时,在 Ah 中,您明确地说该类有一个成员 B。
You MUST include "B.h" in "A.h"
您必须在“Ah”中包含“Bh”
回答by Nuno Ramiro
Aren't you missing the #include "B.h" in A.h?
你不是错过了 Ah 中的 #include "Bh" 吗?
回答by Maghoumi
The solution to my problem today was slightly different that the other answers here.
我今天的问题的解决方案与这里的其他答案略有不同。
In my case, the problem was caused by a missing close bracket (}
) at the end of one of the header files in the include chain.
就我而言,问题是由}
包含链中的一个头文件末尾缺少右括号 ( )引起的。
Essentially, what was happening was that A
was including B
. Because B
was missing a }
somewhere in the file, the definitions in B
were not correctly found in A
.
从本质上讲,发生的事情A
是包括B
. 因为在文件中B
的}
某处丢失了一个,所以B
在A
.
At first I thought I have circular dependency and added the forward declaration B
. But then it started complaining about the fact that something in B
was an incomplete type. That's how I thought of double checking the files for syntax errors.
起初我以为我有循环依赖并添加了前向声明B
。但随后它开始抱怨其中的某些内容B
是不完整的类型。这就是我想到仔细检查文件是否存在语法错误的方式。