【问题标题】:C++ "incomplete type is not allowed" when using stringstream [closed]使用 stringstream 时 C++“不允许不完整类型”[关闭]
【发布时间】:2020-12-24 00:53:25
【问题描述】:

我有以下 C++ 代码

#include <iostream>

int main()
{
    std::string sText = "These are words in my string!";
    std::string sWord;
    std::stringstream ss(sText);
}

它在带有 g++ 的 Fedora 上运行良好。但是,使用 Visual Studio 在 Windows 上运行它(所以我猜是 clang)会得到 incomplete type is not allowed。这是我的代码错误,还是编译器之间存在一些差异? 注意:我试过std::stringstream ss &lt;&lt; sText;,但得到了完全相同的错误。

【问题讨论】:

  • 为什么不尝试在 stringstream 手册中寻找 A 而只是在 SO 上发布 Q 呢?
  • 包括流
  • 要使用std::stringstream,您需要#include &lt;sstream&gt;。所有std:: 名称都是特定的头文件。
  • 要使用std::string,你需要#include &lt;string&gt;

标签: c++ clang stringstream


【解决方案1】:

您未能#include &lt;sstream&gt;

这是允许编译的(任何标准头文件都可以包含任意数量的其他标准头文件),但您通常应该包含您正在使用的“东西”的头文件(并且stringstream&lt;sstream&gt; 中声明)。一旦你包含了头文件,VC++ 就可以很好地编译它。

[Thanks Remy] 既然你用的是std::string,你也应该#include &lt;string&gt;

【讨论】:

  • 同样,您应该 #include &lt;string&gt; 正确使用 std::string
  • @RemyLebeau:好点。
  • 这行得通,但为什么会得到incomplete type is not allowed
  • 显然您包含的其中一个标头声明但未定义stringstream,这使其成为不完整的类型。您可以定义不完整类型的指针或引用,但不能定义其实例。
猜你喜欢
  • 2011-08-12
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
相关资源
最近更新 更多