简体   繁体   English

c ++中的不完整类型

[英]Incomplete type in c++

I have declared DynArray var_name in c++, but while compiling it gives me an error "incomplete type not allowed" I tried to search but nothing came up with an example of dynamic array. 我已经在c ++中声明了DynArray var_name,但是在编译它时给了我一个错误“不允许的不完整类型”我尝试搜索但没有提出动态数组的例子。 Could anyone please explain the error? 有人可以解释一下这个错误吗? Thanks. 谢谢。

If you are defining a member variable x to your class C in a header file Ch , you must include the header of the class X ( Xh ) in Ch . 如果你要定义一个成员变量x到您的类C在头文件Ch ,你必须包括类的头XXh )的Ch If however, you are just storing a pointer to X as a member of C , then you can forward declare class X; 但是,如果你只是存储一个指向X的指针作为C的成员,那么你可以转发声明class X; before you declare class C{...}; 在宣布class C{...}; and then in the definition file C.cpp , you must include Xh if you access any members of x . 然后在定义文件C.cpp ,如果访问x任何成员,则必须包含Xh

"incomplete type not allowed" usually means that a class has been declared but not defined at a point at which its complete definition is needed. “不允许不完整的类型”通常意味着已经声明了一个类,但是在需要完整定义的点上没有定义它。

This sometimes happens because of a circular dependency in the headers, 这有时会因为标题中的循环依赖性而发生,

class A's definition depends on class B's definition and visa-versa - causing Ah to include Bh, and then Bh try's to include Ah but can't because the header guard is already defined, so it skips, but then only gets a fwd decl of A. A类的定义取决于B类的定义,反之亦然 - 导致Ah包含Bh,然后Bh尝试包含Ah但不能因为标题保护已经定义,所以它跳过了,但后来只得到了一个fwd decl一种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM