C++ 结构体的前向声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10894804/
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
Forward declaration of struct
提问by Kenta
I have a header file a.h and inside that I have declared one structure. The name of that structure is file
. Inside file
I have 3 members: a, b, c. In a.cpp I have implemented that structure and assigned some values to that structure variable.
我有一个头文件啊,里面我已经声明了一个结构。该结构的名称是file
. 在里面file
我有 3 个成员:a、b、c。在 a.cpp 中,我实现了该结构并为该结构变量分配了一些值。
Now I have another file b.h. Inside it I have a forward declaration of the structure file
. Until this point if I compile it's not showing an error but when I am going to access the variable present in that structure through that b.cpp class it will give an error like "undefined struct".
现在我有另一个文件 bh 在里面我有一个结构的前向声明file
。到目前为止,如果我编译它不会显示错误,但是当我要通过该 b.cpp 类访问该结构中存在的变量时,它会给出类似“未定义结构”的错误。
What am I doing wrong?
我究竟做错了什么?
回答by Alok Save
What is the root cause of error?
错误的根本原因是什么?
When you Forward declare a type, the compiler treats it as an Incomplete type.
当您 Forward 声明一个类型时,编译器将其视为Incomplete type。
The forward declaration tells the compiler that the said type exists and nothing more about the particular type.So, You cannot perform any action(like creating objects, or dereferencing pointers to that type) on that type which needs compiler to know its memory layout.
前向声明告诉编译器该类型存在并且仅与特定类型相关。因此,您不能对该类型执行任何需要编译器知道其内存布局的操作(例如创建对象或取消引用该类型的指针)。
Solution:
解决方案:
You cannot forward declare if you need to deference the structure members, You will need to include the header file in the source file.This would ensure that the compiler knowsthe memory layout of the type. You will have to design your project accordingly.
如果您需要尊重结构成员,则不能转发声明,您需要在源文件中包含头文件。这将确保编译器知道类型的内存布局。您必须相应地设计您的项目。
回答by Luchian Grigore
To access members, a full definition is required. You need to include
the header inside b.cpp
, not just forward-declare the struct
(which yields an incomplete type).
要访问成员,需要完整定义。您需要include
在 内的标头b.cpp
,而不仅仅是向前声明struct
(这会产生不完整的类型)。
EDIT:
编辑:
A forward declarations is enough for:
前向声明足以用于:
class B;
class C
{
B& b;
B* b;
B foo();
foo(B b);
};
but not for
但不是为了
class B;
class C
{
B b; //ERROR
B foo()
{
B x; //error
x.whatever(); //error
return B(); //error
}
};
回答by Tom Tanner
You need to include the definition of your struct where the members are used or the compiler doesn't know what they are.
您需要包含使用成员的结构的定义,否则编译器不知道它们是什么。
so if you have in b.cpp, this
所以如果你在 b.cpp 中,这个
func(mystruct &s)
{
s.a = 1;
}
The compiler is fine until it gets to the assignment. At which point, it tries to find the definition of 'a' inside 'mystruct', and can't find it.
编译器在执行任务之前很好。此时,它试图在“mystruct”中找到“a”的定义,但找不到。
You should #include "a.h"
in b.cpp, or possibly, depending on what else is in a.h, you might want a separate header for the structure and include that.
您应该 #include "a.h"
在 b.cpp 中,或者可能,这取决于 ah 中的其他内容,您可能需要该结构的单独标头并包含该标头。