不允许不完整的类型:stringstream
为什么这一行给出错误 Error: incomplete type is not allowed
?
stringstream ss;
Why does this line give the error Error: incomplete type is not allowed
?
stringstream ss;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
#include
并使用完全限定名称,即std::stringstream ss;
#include <sstream>
and use the fully qualified name i.e.std::stringstream ss;
某些系统标头提供了
std::stringstream
的前向声明,但没有定义。这使其成为“不完整类型”。要解决此问题,您需要包含
标头中提供的定义:Some of the system headers provide a forward declaration of
std::stringstream
without the definition. This makes it an 'incomplete type'. To fix that you need to include the definition, which is provided in the<sstream>
header:不完整类型
错误是当编译器遇到使用它知道是类型的标识符时,例如因为它已经看到它的前向声明(例如class stringstream;
) code>),但还没有看到它的完整定义(class stringstream { ... };
)。对于您未在自己的代码中使用但仅通过包含的头文件出现的类型,可能会发生这种情况 - 当您包含使用该类型的头文件,而不是定义该类型的头文件时。标头本身不包含其所需的所有标头的情况很不寻常,但并非不可能。
对于标准库中的内容,例如 stringstream 类,请使用该类或单个函数的语言标准或其他参考文档(例如 Unix 手册页、MSDN 库等)来找出需要
#include
来使用它以及在哪个命名空间中找到它(如果有)。您可能需要搜索出现类名的页面(例如man -k stringstream
)。An
incomplete type
error is when the compiler encounters the use of an identifier that it knows is a type, for instance because it has seen a forward-declaration of it (e.g.class stringstream;
), but it hasn't seen a full definition for it (class stringstream { ... };
).This could happen for a type that you haven't used in your own code but is only present through included header files -- when you've included header files that use the type, but not the header file where the type is defined. It's unusual for a header to not itself include all the headers it needs, but not impossible.
For things from the standard library, such as the
stringstream
class, use the language standard or other reference documentation for the class or the individual functions (e.g. Unixman
pages, MSDN library, etc.) to figure out what you need to#include
to use it and what namespace to find it in if any. You may need to search for pages where the class name appears (e.g.man -k stringstream
).